Anthony
Anthony

Reputation: 890

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.

Each row (i.e. tableViewCell) contains different content from different sources/views etc. A number of those rows in the tableView, in turn, contain a CollectionView called SettingsCollectionView. 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. So for example:

tableView row 0 - contains generic content

tableView row 1 - settingsCollectionViewA : SettingsCollectionView as well as a struct OriginalDataA

tableView row 2 - contains generic content

tableView row 3 - settingsCollectionViewB : SettingsCollectionView as well as a struct OriginalDataB

tableView row 4 - settingsCollectionViewC : SettingsCollectionViewas well as a struct OriginalDataC

tableView row 5 - contains generic content

When I initialize settingsCollectionViewA with OriginalDataA, I have a setter in that CollectionView that then sets up the local data under 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.

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.

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. 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.

Upvotes: 0

Views: 78

Answers (2)

Paulw11
Paulw11

Reputation: 114975

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. 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.

Upvotes: 1

Dimple
Dimple

Reputation: 21

Consider you have SettingsCollectionViewA which shows originalDataA. CollectionView displays the data what you provide in cellforrowatindex method. 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.

Upvotes: 1

Related Questions