ios-swift
/

UITableView – Mastering Native Lists

Last Sync: Today

On this page

5
0%
5 min read
Remaining
5 minleft

Click any section to jump — progress syncs automatically

ios-swift

UITableView – Mastering Native Lists

What is a UITableView?

A UITableView displays a single column of vertically scrolling content, divided into sections and rows. As an Architect, you should understand that it doesn't create a view for every item in your data source. Instead, it uses a Reuse Queue to recycle cells that have scrolled off-screen. This is exactly how Flutter's ListView.builder stays memory-efficient.

  1. The Two Core Protocols

To make a table view work, you must implement two protocols. This 'Separation of Concerns' ensures that the view logic is decoupled from the data logic.

  • UITableViewDataSource: Tells the table view what to display (How many rows? Which cell for this index?).
  • UITableViewDelegate: Handles how the table looks and behaves (What happens when I tap a row? How tall is this cell?).

  1. Implementing the Data Source

The most critical method is cellForRowAt. This is where you dequeue a cell and 'hydrate' it with your data. For a Lead Developer, ensuring this method is lightweight is the key to smooth scrolling.

SWIFTRead-only
1
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return projects.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    // Dequeue a reusable cell
    let cell = tableView.dequeueReusableCell(withIdentifier: "ProjectCell", for: indexPath)
    
    // Configure the cell
    let project = projects[indexPath.row]
    cell.textLabel?.text = project.name
    
    return cell
}

  1. Sections and IndexPaths

In iOS, list locations are identified by an IndexPath, which contains both a section and a row. This allows you to easily group content, such as 'Active Projects' in section 0 and 'Archived Projects' in section 1.

UITableView vs. Flutter ListView

FeatureUITableView (Native)ListView (Flutter)
ReactivityManual (reloadData / performBatchUpdates)Automatic (State changes)
Cell RecyclingExplicit (dequeue)Implicit (Viewport-based)
Grouped StylesNative 'Grouped' or 'Inset' stylesManual styling with Slivers
InteractionsBuilt-in Swipe-to-DeleteRequires Dismissible widget
ComplexityHigh (Requires delegate setup)Low (Declarative)

Test Your Knowledge

Q1
of 3

Which protocol is responsible for providing the number of rows and the cell content to a UITableView?

A
UITableViewDelegate
B
UITableViewDataSource
C
UITableViewCell
D
UIScrollViewDelegate
Q2
of 3

What is the primary benefit of 'dequeuing' a cell instead of creating a new one?

A
It makes the cell look better
B
It saves memory and CPU by reusing views that are off-screen
C
It automatically saves data to the cloud
D
It allows the cell to be resized automatically
Q3
of 3

Which object represents the specific location (section and row) of a cell in a table view?

A
IndexPoint
B
RowLocation
C
IndexPath
D
CellPosition

Frequently Asked Questions

What is the 'reuseIdentifier'?

It is a string key that identifies the type of cell to be reused. If you have different designs for 'Header' cells and 'Content' cells, you give them different identifiers so the table view doesn't try to reuse a Header design for a Content row.

How do I update the list after data changes?

The simplest way is calling 'tableView.reloadData()'. However, for better performance and smooth animations, Architects use 'performBatchUpdates' or the modern 'UITableViewDiffableDataSource' (introduced in iOS 13).

Why is my list scrolling choppy?

Usually because 'cellForRowAt' is doing too much work. Avoid image decoding, complex math, or database calls inside this method. Do that work in the background and only assign the final result to the cell's UI elements.

Previous

ios app lifecycle

Next

ios swiftui

Related Content

Need help?

Explore our comprehensive docs or start a chat with our tech experts.