Using Auto Layout in UITableView for dynamic cell layouts & variable row heights
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
UITableView can calculate row heights automatically when the cell's content view has enough Auto Layout information to determine its vertical size. The two key ingredients are tableView.rowHeight = UITableView.automaticDimension and a cell layout that has an unbroken top-to-bottom constraint chain. If the constraints are incomplete or ambiguous, dynamic height stops being reliable fast.
Enable Automatic Dimension First
The table view must be told to use self-sizing rows.
estimatedRowHeight does not need to be exact. Its job is to help the table estimate content size before the actual cells are measured.
Without automaticDimension, the rest of the Auto Layout work will not produce dynamic row heights.
Build the Cell with a Clear Vertical Constraint Chain
The cell's subviews must allow Auto Layout to infer the total height from the content.
For a simple title-plus-body cell, the vertical chain is usually:
- top of content view to title label
- title label to body label
- body label to bottom of content view
The labels also need proper horizontal constraints.
A programmatic cell example:
That bottom anchor on bodyLabel is essential. Without a constraint that reaches the bottom of the content view, the cell height is often ambiguous.
Configure the Table Normally
Once the cell layout is correct, ordinary table view configuration is enough.
If the labels and constraints are correct, the long text row grows automatically.
Common Design Rules for Self-Sizing Cells
A few rules make self-sizing much more reliable:
- set multiline labels to
numberOfLines = 0 - constrain subviews fully in both horizontal and vertical directions
- anchor the last vertical element to the bottom of
contentView - avoid manually setting frames on Auto Layout-managed subviews
- prefer constraints inside
contentView, not on the cell itself
These rules matter more than the exact visual design.
When Manual Height Calculation Is Usually Unnecessary
Older table view code often used heightForRowAt. With modern Auto Layout and self-sizing cells, that is often unnecessary. You should implement manual height calculation only when:
- the layout is too complex for automatic sizing performance-wise
- you already have a proven manual measurement path
- you need a highly specialized layout optimization
For most content-driven cells, automatic dimension is simpler and easier to maintain.
Common Pitfalls
A common mistake is forgetting the bottom constraint from the last subview to the cell's contentView. Without it, the system cannot infer the final height correctly.
Another mistake is leaving multiline labels at their default single-line setting. Then long text truncates instead of expanding the cell.
People also often add constraints to the cell instead of contentView, which leads to confusing layout behavior.
Finally, mixing manual frame changes with Auto Layout constraints usually causes unpredictable results in self-sizing cells.
Summary
- Dynamic row height in
UITableViewdepends on bothautomaticDimensionand a complete Auto Layout cell layout - Use an estimated row height and let the table compute the final height automatically
- Build an unbroken top-to-bottom constraint chain inside the cell's
contentView - Set multiline labels to
numberOfLines = 0when text should wrap - Prefer self-sizing cells over manual
heightForRowAtunless you have a clear reason not to - Most failures come from incomplete constraints, not from the table view API itself

