swiftuitableviewreuseidentifier

Why do my TableView cells need reuse identifiers?


To practice my programming and keep track of my progress I'm building an app for myself. I've created a tableview and for each section and independent cells for each requirement. Now Xcode is upset because each prototype cell doesn't have an identifier.

I've looked at the Swift docs and gone through Youtube vids on the topic but I still don't understand what reuse identifiers are or what they are used for, let alone how it would help in my case.

Screenshot


Solution

  • If looks like you're creating a new cell for each value (“Array functions”, “Cocoapods”, etc.). If using cell prototypes, you wouldn't generally do that. You would generally have a single prototype cell, say, one with a reuse identifier of SkillCell, which would have a center aligned label. So, you would not see all of these different skills in your storyboard, but rather your table view’s data source would provide the values, instantiating your reused SkillCell cell, passing it the skill name:

    So your storyboard might look like

    enter image description here

    And then you'd populate it programmatically:

    struct Skill {
        let name: String
    }
    
    struct SkillLevel {
        let name: String
        let skills: [Skill]
    }
    
    class ViewController: UIViewController {
        let sections = [
            SkillLevel(name: "Minimum Required Skills", skills: [
                Skill(name: "Array Functions"),
                Skill(name: "CocoaPods"),
                Skill(name: "API Requests"),
                Skill(name: "JSON")
            ]),
            SkillLevel(name: "Intermediate Required Skills", skills: [
                Skill(name: "Grand Central Dispatch"),
                Skill(name: "Combine"),
                Skill(name: "MVC vs MVP vs MVVM vs MVVM-C")
            ])
        ]
    }
    
    extension ViewController: UITableViewDataSource {
        func numberOfSections(in tableView: UITableView) -> Int {
            return sections.count
        }
    
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return sections[section].skills.count
        }
    
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "SkillCell", for: indexPath) as! SkillCell
            cell.skillLabel.text = sections[indexPath.section].skills[indexPath.row].name
            return cell
        }
    }
    
    extension ViewController: UITableViewDelegate {
        func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
            return sections[section].name
        }
    }
    

    Yielding:

    enter image description here

    Now, if you want to add all of these skills in IB, then fine, you can do that, but you wouldn't generally use prototype cells (each with their own reuse identifier), but rather static table view cells.