EDIT: It seems I wasn’t completely clear in my meaning of "small/fixed number of cells". What I mean is that the number of cells is unchanging (there is always only 10 cells). However, the cells’ contents are very dynamic. I have a complex UITableViewCell class to handle this, and it’s difficult for me to "capture" the exact state of every element and variable at a given time for the "reusing" to work correctly.
I know it’s highly frowned upon… but I would like to create UITableViewCells that are not reusable. The reason I have decided on this is because I only need a small/fixed number of cells (10 to be exact), and they are pretty complex, so I feel like having to capture their various states before they are reused, and then resetting these states when each cell is shown, would be excessively tedious, especially since memory doesn’t seem like it will be an issue with only ten non-reusable cells.
However, I am confused how to do this (there is very limited online resources on achieving this unorthodox task). As a simplified test, I tried to simply create an instance of my CustomTableViewCell in the cellForRowAt
data source method, but the cell appears completely blank. It does not appear that any of the IBOutlets were established prior to the cell being displayed (I posted a print()
statement in the cell’s awakeFromNib()
method and it was never called).
Does anyone know what steps I am missing and/or doing incorrectly?
For reference, here is my table view data source’s cellForRowAt
method:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Assuming for the sake of the argument that there is only one row (so only one cell needed)...
let cell = CustomTableViewCell()
return cell
}
I have also tried creating the cell with let cell = CustomTableViewCell(style: .default, reuseIdentifier: "customCell")
but this didn’t work either.
Any help would be appreciated–thanks!
3
Answers
For making your cell static, once you have to create tableViewController go for attribute inspector make the content type from dynamic to static. so you can fix the number of cells to particular number(mentioned below in 1st and 2nd picture )..
enter image description here
enter image description here
Not sure if this answers your question, but you might try to call the override func prepareForReuse() in your CustomTableViewCell().
For example, if you want to add a switch in a table view, to avoid that the reusable cell will mess all switches in your list, you can do like this :
A little clarification …
Just because you use a reusable cell doesn’t mean you have to do anything "dynamic" to it.
Assuming you are designing your cells in Storyboard, you may have something like this:
I’ve designed 5 "complex static" cells. They look exactly how I want them displayed, so no custom cell classes or
@IBOutlet
connections.All I’ve done is given each one a different
identifier
… in this case, I used "type1" / "type2" / "type3" / "type4" / "type5".Now, a simple controller class:
Notice that in
cellForRowAt
I use the data source value to generate a reuse identifier:and it loads (dequeues) the correct "static" cell, and I get this output:
If I change my data source array to:
I get this:
Using
theData = [5, 4]
:and using
theData = [4, 3, 5, 1, 2]
:As you can see, the cells are "static" but I can display only the ones I want, in the order I want.
If you are designing your cells in
xib
files, add this toviewDidLoad()
:Obviously, replace the "type" strings with strings that make sense to your code.