简体   繁体   English

在 Swift 中使用 TableViews 和 CollectionViews 并管理多个数据源

[英]Working with TableViews and CollectionViews and managing multiple data sources in Swift

I have a main ViewController, MainViewController that contains a number of views along with a TableView .我有一个主 ViewController, MainViewController包含许多视图和一个TableView

Each row (ie tableViewCell ) contains different content from different sources/views etc. A number of those rows in the tableView, in turn, contain a CollectionView called SettingsCollectionView .每行(即tableViewCell )包含来自不同来源/视图等的不同内容。 tableViewCell的许多行又包含一个名为SettingsCollectionViewCollectionView In the tableView 's cellForRowAt method, I initialize the SettingsCollectionView for that tableViewCell and also pass it the respective data which that CollectionView subclass uses as its data source.tableViewcellForRowAt方法中,我初始化了该tableViewCellSettingsCollectionView并将CollectionView子类用作其数据源的相应数据传递给它。 So for example:例如:

tableView row 0 - contains generic content tableView第 0 行 - 包含通用内容

tableView row 1 - settingsCollectionViewA : SettingsCollectionView as well as a struct OriginalDataA tableView第 1 行 - settingsCollectionViewA : SettingsCollectionView以及一个结构OriginalDataA

tableView row 2 - contains generic content tableView第 2 行 - 包含通用内容

tableView row 3 - settingsCollectionViewB : SettingsCollectionView as well as a struct OriginalDataB tableView第 3 行 - settingsCollectionViewB : SettingsCollectionView以及一个结构OriginalDataB

tableView row 4 - settingsCollectionViewC : SettingsCollectionView as well as a struct OriginalDataC tableView第 4 行 - settingsCollectionViewC : SettingsCollectionView以及一个结构OriginalDataC

tableView row 5 - contains generic content tableView第 5 行 - 包含通用内容

When I initialize settingsCollectionViewA with OriginalDataA , I have a setter in that CollectionView that then sets up the local data under LocalDataA .当我用OriginalDataA初始化settingsCollectionViewA时,我在CollectionView中有一个 setter,然后在LocalDataA下设置本地数据。 This allows me to ensure I have the original and the working copy of the data based on the user making changes etc. Any time I call the OriginalDataA variable from the MainViewController , a getter in settingsCollectionViewA does some cleaning up of the data etc. so I can then do what I want with it in the MainViewController .这使我能够根据用户进行的更改等确保我拥有数据的原始副本和工作副本。每当我从MainViewController调用OriginalDataA变量时, settingsCollectionViewA中的 getter 都会对数据进行一些清理等,所以我然后可以在MainViewController使用它做我想做的事情。

That part all works well except if those tableView cells are dequeued, when they reappear, I get back the original state for that tableViewCell and in turn collectionView rather than the state the user left it in.这部分一切正常,除非那些 tableView 单元格出列,当它们重新出现时,我会恢复该tableViewCell的原始状态,然后是 collectionView 而不是用户留下的状态。

I realize this is because each SettingsCollectionView class is working its own local copy of OriginalDataA , OriginalDataB etc. and appreciate I can just update the original data but then that creates other complexities - like the cleanness of current 'standalone' code built for the SettingsCollectionView subclass as well as the complexity of original vs. updated data.我意识到这是因为每个SettingsCollectionView类都在使用自己的OriginalDataAOriginalDataB等本地副本,并且很感激我可以只更新原始数据,但这会产生其他复杂性 - 例如为SettingsCollectionView子类构建的当前“独立”代码的清洁度以及原始数据与更新数据的复杂性。 That's why I am stuck on a better programming approach...这就是为什么我坚持使用更好的编程方法......

Apologies, this might seem like a basic question but I'm new to programming and all the examples etc. I can find all speak to more simple scenarios rather than what I'm trying to do.抱歉,这似乎是一个基本问题,但我是编程和所有示例等的新手。我发现所有这些都涉及更简单的场景,而不是我想要做的事情。

I haven't included the code because it's got a whole lot of other content and functionality that I think just confuses the concept outlined above.我没有包含代码,因为它有很多其他的内容和功能,我认为这些内容和功能只会混淆上面概述的概念。

Any help would be greatly appreciated.任何帮助将不胜感激。

Consider you have SettingsCollectionViewA which shows originalDataA .考虑您有SettingsCollectionViewA显示originalDataA CollectionView displays the data what you provide in cellforrowatindex method. CollectionView 显示您在 cellforrowatindex 方法中提供的数据。 Collection view cells reuse memory every time.集合视图单元每次都重用内存。 Only visible cells stays in the memory at any point of time.在任何时间点都只有可见的单元格留在内存中。 So user modified data will not be stored explicitly unless you modify the originalDataA.所以用户修改的数据不会被显式存储,除非你修改了 originalDataA。

Create a class that holds each of your data structs:创建一个包含每个数据结构的类:

class MyDataModel {
    var dataA: DataA
    var dataB: DataB
    var dataC: DataC
}

Then you create an instance of MyDataModel and hold a reference to it in a property of your view controller.然后创建一个MyDataModel实例并在视图控制器的属性中保存对它的引用。 Pass this same instance to your table view cells.将此相同的实例传递给您的表格视图单元格。 Since it is a class and therefore a reference type, changes made by the cell will actually be made in this one instance.由于它是一个类,因此是一个引用类型,因此单元格所做的更改实际上将在此实例中进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Swift中组合的TableViews和CollectionViews - Combined TableViews & CollectionViews in Swift iOS-对视图中的多个Tableview或CollectionView使用/传递手势识别器(Swift) - iOS - Use/pass gesture recognizer for multiple Tableviews or Collectionviews in a View(Swift) 一个视图中有多个TableView-数据源数组? - Multiple TableViews in one View - Array of data sources? TableViews无法与TabBarController Swift一起使用 - TableViews not working with TabBarController Swift 使用多个表视图和数据源 - Working multiple tableviews and datasources 在多个CollectionView之间滑动 iOS Swift - Swipe between multiple CollectionViews in iOS Swift 在表视图中填充数据,swift2 - Fill tableviews in view with data, swift2 Swift 5 - 让 tableview 等到来自 api 调用的数据返回(使用多个 tableviews) - Swift 5 - make tableview wait until data from api call comes back (using multiple tableviews) 如何将标签设置为UICollectionView-SWIFT-同一屏幕上的多个CollectionView - How to set a tag to UICollectionView - SWIFT - Multiple CollectionViews on same screen 我如何解析来自 UIViewController 的数据,该数据具有 2 个 CollectionViews 和 swift - How can I parse data from a UIViewController having 2 CollectionViews with swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM