How to not reuse TableViewCells, but still use prototypes

I have a tableview where a single prototype cell is used only 3 times. This cell has some buttons on it that trigger displaying and hiding content within the cell.


I could reuse the cells, but that requires a lot of resetting each cell so that if I expand A's content, B's content isn't expanded when it loads. Furthermore, this requires me to keep a state record in the Controller class, where I would prefer to have this all handled by the Cell itself for modularity. In other words, the amount of work to keep each cell in the correct stage seems inefficient.


What would be the best way to go about this? Do I use static cells? Is there a way to instance 3 separate cells of the same type and place them in the TableView?

Replies

If I understand correctly what you want to do, you can define 3 different cell prototype, each with its own identifier.


And refer to the right identifier in


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


when dequeuing the cell (with the appropriate class if they are different for each type of cell).

No, I have 1 prototype cell that I want to use 3 different times. But this cell prototype has some different states (expanded and collapsed) that I would prefer to be maintained by itself. I'm thinking that I will need to create a tableview with static cells, and have 3 instances of the same cell...

Could you show the code of

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell


And explain what is different depending on the state of the cell.

Under Model-View-Controller what you want to do is sounds exactly wrong. I suspect that the small bit of time you are saving by burying your Model information in the View will come back to bite you hard. Reuse the cell; don't 'reset' anything; 'set' everything. And keep a record in a Model of what you have set in the View.