[英]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
的许多行又包含一个名为SettingsCollectionView
的CollectionView
。 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.在
tableView
的cellForRowAt
方法中,我初始化了该tableViewCell
的SettingsCollectionView
并将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
类都在使用自己的OriginalDataA
、 OriginalDataB
等本地副本,并且很感激我可以只更新原始数据,但这会产生其他复杂性 - 例如为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.