UITableView example for Swift
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Introduction
UITableView is the most commonly used component in iOS for displaying scrollable lists of data. It requires a data source (providing cells and section info) and optionally a delegate (handling selection and layout). This article shows a complete, working example of a UITableView in Swift — from basic setup through custom cells, sections, swipe actions, and the modern diffable data source API.
Basic UITableView Setup
This is the minimum setup: register a cell, implement two data source methods, and handle selection.
Custom UITableViewCell
Register and use in the view controller:
Sections
Swipe Actions
Pull to Refresh
Common Pitfalls
- Forgetting to register the cell: Calling
dequeueReusableCell(withIdentifier:for:)without registering the cell class or nib first causes a crash. Always calltableView.register(CellClass.self, forCellReuseIdentifier:)inviewDidLoad. - Adding subviews to
cellinstead ofcell.contentView: UI elements must be added tocontentView, not the cell directly. Adding to the cell causes layout issues with editing mode, swipe actions, and accessory views. - Not calling
dequeueReusableCell: Creating a new cell every time instead of dequeuing wastes memory and causes scroll performance issues. Always dequeue and configure the returned cell. - Forgetting
reloadDataafter data changes: Modifying the data array without callingtableView.reloadData()(orinsertRows/deleteRows) leaves the table view out of sync, causing crashes on scroll. - Cell reuse not resetting state: Dequeued cells retain state from their previous use. If a cell has an image and the new data has no image, the old image persists. Always reset all UI elements in
cellForRowAtor overrideprepareForReuse().
Summary
- Implement
UITableViewDataSourcefornumberOfRowsInSectionandcellForRowAt(minimum required) - Implement
UITableViewDelegatefordidSelectRowAtand layout customization - Always register cell classes before dequeuing — use
tableView.register() - Create custom
UITableViewCellsubclasses for complex layouts with Auto Layout - Add subviews to
contentView, not the cell directly - Use swipe actions, pull-to-refresh, and sections to build feature-rich table views
Related reading
- UITableView hide header from empty section
- UITableview How to Disable Selection for Some Rows but Not Others
- UITableView load more when scrolling to bottom like Facebook application
- UITableView not showing first time, only when scrolling a bit
- UITableview reloaddata with animation
- UITableView row animation duration and completion callback
- UITableView, Separator color where to set?
- UITableView set to static cells. Is it possible to hide some of the cells programmatically?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.