Skip to content
Prepare to Grow

UIKit Development

Table Views

Present efficient vertical lists with reusable cells, self-sizing content, selection, and clean data ownership.

Intermediate5 min read2 guided activities

Before you begin

What you will understand

  • Explain cell reuse and index-path roles.
  • Implement table data source and delegate behavior.
  • Build self-sizing accessible rows.
  • Keep displayed cells derived from a source of truth.
01

Learning stage

Concept Explanation

UITableView presents a vertically scrolling sequence of sections and rows while reusing a limited pool of cells. The data source reports structure and configures cells; the delegate handles selection and display behavior. An index path describes a current location, not durable item identity.

Register a cell class or use a cell registration, dequeue for every requested index path, and fully configure every visible state because a reused cell retains prior values. With a complete vertical constraint chain and automatic dimensions, rows can self-size for Dynamic Type and variable content.

Selection should resolve the current model and emit a semantic action. The model collection remains the source of truth; views do not own domain data. For complex animated mutations, diffable data sources reduce manual coordination, which a later lesson covers in depth.

  • Data source: provides section and row content.
  • Delegate: responds to selection and display behavior.
  • Reuse identifier: groups cells with the same structure.
  • Index path: temporary section-and-row location.
  • Self-sizing cell: derives height from content and constraints.
02

Learning stage

Syntax & Examples

A simple table controller fully configures each reused list cell from immutable row data.

LessonListViewController.swiftswift
final class LessonListViewController: UITableViewController {
  private var lessons: [Lesson] = []

  override func viewDidLoad() {
    super.viewDidLoad()
    tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Lesson")
    tableView.rowHeight = UITableView.automaticDimension
    tableView.estimatedRowHeight = 64
  }

  override func tableView(_ tableView: UITableView,
                          numberOfRowsInSection section: Int) -> Int {
    lessons.count
  }

  override func tableView(_ tableView: UITableView,
                          cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Lesson", for: indexPath)
    let lesson = lessons[indexPath.row]
    var content = cell.defaultContentConfiguration()
    content.text = lesson.title
    content.secondaryText = lesson.durationText
    cell.contentConfiguration = content
    cell.accessoryType = lesson.isComplete ? .checkmark : .disclosureIndicator
    return cell
  }
}
Every reuse pass sets content and accessory state, preventing stale visual values.
03

Learning stage

Real-World Usage

Connect the UIKit concept to complete, maintainable application features.

  • Display settings, search results, messages, or grouped records.
  • Show loading, empty, content, and error states around a list.
  • Support swipe actions and contextual menus where appropriate.
  • Build rows that adapt to accessibility text sizes.
04

Learning stage

Common Mistakes

Identify lifecycle, ownership, identity, layout, accessibility, and user-experience risks.

  • Treating index paths as stable identifiers.
  • Failing to reset conditional properties on reused cells.
  • Creating a new cell without dequeuing.
  • Using fixed row heights for multiline Dynamic Type content.
  • Changing the table before updating its model.
05

Learning stage

Practice Exercises

Build the behavior in a small Xcode project and inspect it across realistic states.

Activity 1

Build a sectioned lesson list

Group lessons by module and display headers, completion accessories, and selection callbacks.

Hint: Create a section model instead of calculating groups repeatedly in data-source methods.

Activity 2

Audit cell reuse

Add optional badges and images, scroll rapidly, then ensure every conditional property is reset during configuration.

Hint: Test rows with and without each optional value.

06

Learning stage

Knowledge Check

Answer each question before revealing the explanation.

QUESTION 1

Why must a reused cell be fully configured every time?

  1. A.It may still contain state from a previously displayed item
  2. B.It has no view hierarchy
  3. C.It cannot use Auto Layout
  4. D.It always represents the same model
Reveal answer

It may still contain state from a previously displayed item

Reuse changes which model a cell represents, so all visible state must be derived again.

QUESTION 2

Why is an index path not a stable model identifier?

Reveal answer

Its location can change when sections or rows are inserted, removed, sorted, or filtered.

Use a stable model ID when work must remain attached to the same item.

07

Learning stage

Interview Preparation

Explain the design in terms of lifecycle, identity, ownership, system behavior, and trade-offs.

How does UITableView reuse work, and what bugs does it create?

Short answer: The table recycles offscreen cells, so configuration must overwrite every state and asynchronous work must follow stable identity.

Explore the detailed answer

Only enough cells for the visible region and a buffer are created. As cells are reused for new index paths, stale labels, accessories, images, or tasks can appear unless configuration is complete and cancellation/identity checks are in place. The model, not the cell, remains authoritative.

Possible follow-ups

  • • How do self-sizing rows work?
  • • When would you choose a collection view instead?
08

Learning stage

Key Takeaways

Table views efficiently display vertical data by reusing cells. Reliable implementations derive every cell state from the model and treat index paths as temporary locations.

  • Reuse cells instead of creating one per item.
  • Fully configure all visible state.
  • Keep domain data outside cells.
  • Treat index paths as locations, not identity.
  • Use complete constraints for self-sizing content.