Reputation: 1156
I have recently become aware that when using a UITableView, I should be managing all my cells' data outside of the actual cell objects (to "persist" my data since cells are reused). Then, when I need to provide a cell to my tableview in my cellForRowAt
method, I simply set that "persisted" data to my reused cell.
However, I am confused specifically on how to store changes in user-interaction UIelements in my cells? (For example, if a UILabel's alpha value changes from a default 1.0 to a new value of 0.5)
Here are a couple possible ideas I've had (which I'm not convinced are correct, so any critiques or suggestions are welcome):
Replicate each IBOutlet variable of the cell entirely in the cell's data model (meaning I would have a UILabel in the UITableViewCell, and a persisted copy in my CellModel). Then I can just copy these entire variables back to the cell in the cellForRowAt
method. However, this seems kind of redundant and a waste of memory.
Just record the specific change in my cell' model. For example, if an alpha value of a UILabel changes, just record that specific new alpha value, not the whole UILabel. However, the problem/confusion I'm seeing with this is that it seems like I might be recording a lot of things for just one element (if more than just an alpha changes for a particular element). I am also confused on how I should actually implement this (so maybe this could be a lot easier than I'm visualizing?).
Appreciate the guidance
ps. I use storyboard
Upvotes: 0
Views: 62
Reputation: 4376
There should never be a case where you would need to store UITableViewCell
UI variable state inside cell data model, you can do it tho, but it is highly discouraged.
Usually your UI state (not only UITableViewCell
s, any UI element) depends on some data model it represents. So, in your case, data model of a cell could have a boolean/enum or whatever else to represent a state of a cell and you could compose your UITableViewCell
's depending on their data model states.
Upvotes: 2