skip to Main Content

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


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

    Login or Signup to reply.
  2. 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 :

    override func prepareForReuse() {
        super.prepareForReuse()
        // declare the switch with the default state
        mySwitch.isOn = false
     
    }
    
    Login or Signup to reply.
  3. 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:

    enter image description here

    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:

    class RowTypesTableViewController: UITableViewController {
        
        var theData: [Int] = []
        
        override func viewDidLoad() {
            super.viewDidLoad()
    
            theData = [1, 2, 3, 4, 5]
    
        }
        
        override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return theData.count
        }
        
        override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let id: String = "type(theData[indexPath.row])"
            let cell = tableView.dequeueReusableCell(withIdentifier: id, for: indexPath)
            return cell
        }
        
    }
    

    Notice that in cellForRowAt I use the data source value to generate a reuse identifier:

    let id: String = "type(theData[indexPath.row])"
    

    and it loads (dequeues) the correct "static" cell, and I get this output:

    enter image description here

    If I change my data source array to:

    theData = [1, 3, 4]
    

    I get this:

    enter image description here

    Using theData = [5, 4]:

    enter image description here

    and using theData = [4, 3, 5, 1, 2]:

    enter image description here

    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 to viewDidLoad():

        tableView.register(UINib(nibName: "type1", bundle: nil), forCellReuseIdentifier: "type1")
        tableView.register(UINib(nibName: "type2", bundle: nil), forCellReuseIdentifier: "type2")
        tableView.register(UINib(nibName: "type3", bundle: nil), forCellReuseIdentifier: "type3")
        tableView.register(UINib(nibName: "type4", bundle: nil), forCellReuseIdentifier: "type4")
        tableView.register(UINib(nibName: "type5", bundle: nil), forCellReuseIdentifier: "type5")
        
    

    Obviously, replace the "type" strings with strings that make sense to your code.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search