UITableview with more than One Custom Cells with Swift
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Using more than one custom cell type in a UITableView is normal once the screen shows mixed content such as text rows, image rows, or action rows. The key is to make cell selection data-driven instead of scattering if indexPath.row == ... logic throughout the controller.
Model the Rows First
The cleanest approach is to define a row model that describes which cell type belongs at each position. An enum works well for this.
Once the data source uses RowItem, the table view can switch on the row type and dequeue the matching cell class. That is much easier to maintain than hardcoding cell decisions by row number alone.
Create One Subclass per Cell Type
Each custom cell should own only the outlets and configuration logic for its own layout.
You can register these cells with nibs, storyboard prototypes, or direct class registration depending on how the screen is built.
Register and Dequeue by Identifier
The controller should register both cell types and then choose one in cellForRowAt.
This is the core pattern. The table view does not care that there are multiple custom cells; it only needs the right registration and dequeue logic.
Row Height and Layout
If the cells have different heights, use Auto Layout and enable self-sizing cells, or implement heightForRowAt if the heights are fixed.
For self-sizing:
Self-sizing works best when each cell's constraints fully describe its height. If constraints are incomplete, mixed cell types often expose the layout problem quickly.
Multiple Sections Are Still an Option
Sometimes a second cell type is really a second section, not a second row style. If the data has a natural grouping, use sections instead of forcing all variation into one flat row list.
That said, when the content is interleaved in one feed, an enum-based row model remains the simplest design.
Avoid Controller-Centric Configuration
A common mistake is putting all label text and image logic directly in cellForRowAt. That makes the controller grow into a formatting object.
Keep the controller responsible for:
- choosing the cell type
- passing model data into the cell
Keep the cell responsible for:
- outlet ownership
- applying fonts, colors, and image content
That split becomes more valuable as the table adds a third or fourth custom cell type.
Common Pitfalls
The most common mistake is registering one identifier and trying to cast it to multiple cell classes. Each cell type needs its own reuse identifier.
Another mistake is letting reuse artifacts leak between cells. A reused cell may still show stale content if configure does not fully update every visible field.
Developers also overuse indexPath.row checks instead of modeling the row type in data. That approach works briefly and becomes fragile as soon as rows are inserted or reordered.
Finally, mixed cell heights often expose broken Auto Layout constraints. If one cell type renders incorrectly, inspect the cell's internal constraints before blaming the table view.
Summary
- A
UITableViewcan support multiple custom cell types without special framework tricks. - Model row types explicitly, usually with an enum or view model.
- Register one reuse identifier per custom cell class or nib.
- Dequeue the correct cell in
cellForRowAtby switching on the row model. - Keep cell-specific layout and display logic inside the cell subclass, not in the controller.
Related reading
- UITableViewCell Separator disappearing in iOS7
- UITableViewCell, show delete button on swipe
- UITableViewCell show white background and cannot be modified on iOS7
- UITableViewCell subview disappears when cell is selected
- UITableViewCell with UITextView height in iOS 7?
- UITableViewHeaderFooterView Unable to change background color
- UITapGestureRecognizer - make it work on touch down, not touch up?
- UITapGestureRecognizer - single tap and double tap
.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 courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.