Setting custom UITableViewCells height
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Custom table view cell height in UIKit can be either fixed, computed per row, or derived automatically from Auto Layout. The right choice depends on whether your content is predictable or variable, and many layout bugs come from mixing those approaches without a clear rule.
Fixed Height for Uniform Rows
If every cell in the table should have the same height, set a constant height through the table view delegate.
This is simple and predictable. If the design has identical rows with fixed-size content, a hard-coded height is often perfectly fine.
Different Heights for Different Row Types
Many screens mix cell styles, such as a compact summary row and a larger detail row. In that case, return a height based on your row model.
This is better than scattering row-number checks throughout the code. Heights should follow the meaning of the row, not its temporary position.
Self-Sizing Cells with Auto Layout
If the content length changes, such as multi-line labels or dynamic text sizes, self-sizing cells are usually the better solution. Apple’s Auto Layout guidance for self-sizing cells is to set rowHeight to automatic dimension and provide an estimated height.
For that to work, the cell’s subviews must have a complete vertical constraint chain from the content view’s top to bottom.
Example custom cell:
If the label can grow vertically and the constraints are correct, the table view computes the cell height automatically.
When to Avoid heightForRowAt
Once you adopt self-sizing cells, avoid returning fixed heights from heightForRowAt unless you intentionally want to override Auto Layout. If you force a number there, you are telling the table view to stop asking Auto Layout for the real answer.
Use heightForRowAt when:
- height is fixed
- height depends on row type in a known way
Use automaticDimension when:
- content length changes
- Dynamic Type should resize text cleanly
- labels, images, or stacks can expand vertically
Mixing both without a clear reason often creates confusing bugs.
Performance Considerations
Self-sizing is powerful, but it is not free. The more complicated the cell hierarchy and constraints, the more work Auto Layout must do while scrolling.
Practical rules:
- provide a realistic
estimatedRowHeight - keep the constraint graph simple
- avoid expensive work in
layoutSubviews - configure reusable cells fully so old state does not affect measurement
Most apps should still choose self-sizing when the content is genuinely dynamic. The performance cost of correct sizing is usually lower than the maintenance cost of manual height math.
Common Pitfalls
The biggest mistake is turning on automaticDimension without giving the cell a complete vertical constraint setup. The table view cannot infer a height from incomplete constraints.
Another mistake is implementing heightForRowAt and expecting self-sizing to still decide the final height. The delegate method wins.
Developers also forget estimatedRowHeight. The table may still work, but scrolling and initial layout become less smooth because the system has poor estimates.
Finally, do not compute dynamic height by measuring text manually unless you have a strong reason. UIKit and Auto Layout already solve that problem well when the cell is constrained properly.
Summary
- Use
heightForRowAtfor fixed or clearly rule-based cell heights. - Use
UITableView.automaticDimensionfor content-driven heights. - Self-sizing cells require a complete vertical Auto Layout chain inside the content view.
- Do not mix self-sizing and forced delegate heights accidentally.
- A good estimated height improves perceived scrolling performance.
Related reading
.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.