UIKitMidMCQ

What is `UITableView` reuse and why does it matter?

Test your knowledge:

Explanation & Code

Answer: UITableView reuses cell objects as they scroll off screen rather than creating new ones, keeping memory usage constant regardless of how many rows exist.

Code Example:

class MyViewController: UIViewController, UITableViewDataSource {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Register cell class with a reuse identifier
        tableView.register(UserCell.self, forCellReuseIdentifier: "UserCell")
    }

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

        // Dequeue a recycled cell (or create one if none available)
        let cell = tableView.dequeueReusableCell(
            withIdentifier: "UserCell", for: indexPath) as! UserCell

        // ALWAYS configure fully — recycled cells carry old data
        let user = users[indexPath.row]
        cell.nameLabel.text = user.name
        cell.avatarImageView.image = nil  // reset before async load

        return cell
    }
}

Common mistake: Not resetting cell state before configuring — old data shows briefly during scroll.

Rate your understanding:

Ready to practice more UIKit?

Test yourself with our interactive quiz mode or browse all curated questions for this topic.