How do I wrap text in a UITableViewCell without a custom cell
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
You do not need a custom cell just to show multi-line text in a table view. The built-in UITableViewCell can wrap long strings correctly as long as you configure the label for multiple lines and let the row height grow with the content.
Configure the Default Cell for Wrapping
The core requirement is simple: the cell's text label must allow unlimited lines and use word wrapping. On current iOS versions, the cleanest approach is to use a default content configuration.
numberOfLines = 0 tells the label to use as many lines as it needs. Without that one line, long text will truncate even if the row height is large enough.
Let the Table View Calculate the Height
Wrapping alone is not enough. The table view must also be allowed to size each row based on the label's content. That is what automaticDimension is for.
With self-sizing rows enabled, the built-in layout of the cell can expand vertically. The estimate does not need to be exact, but giving the table a reasonable estimate improves scrolling performance because the table view can plan its content size more accurately.
Support Older Code That Uses textLabel
If your codebase still uses the traditional textLabel property, the same idea applies. You configure the label directly and still rely on automatic row height.
This still avoids a custom subclass. It is enough for plain text lists, error messages, settings descriptions, and similar content where one label is all you need.
Know When the Default Cell Stops Being Enough
The standard cell works well when the layout is simple, but it is not the right tool for every design. If you need multiple labels, images with custom spacing, or highly styled content, a custom cell becomes easier to maintain than fighting the defaults.
The practical rule is straightforward: if one wrapped text block solves the problem, stay with the built-in cell. If you are manually repositioning subviews or adding layout constraints to the cell's content view, you have already crossed into custom-cell territory.
Common Pitfalls
- Setting
numberOfLines = 0but leavingtableView.rowHeightfixed, which causes the text to wrap internally and then get clipped. - Reusing a cell without reapplying the text configuration, which can make different rows show stale line settings.
- Using
sizeToFiton the label inside a table view cell, which fights the table view's own sizing system. - Initializing the wrong cell style and expecting a label that is not present in that style configuration.
- Forcing a custom frame layout when
automaticDimensionand the built-in label constraints already solve the problem.
Summary
- A custom cell is not required for a single wrapped text label.
- Set the label to unlimited lines and enable word wrapping.
- Use
UITableView.automaticDimensionso row height follows the content. - Prefer
defaultContentConfiguration()in modern code andtextLabelonly for older code paths. - Switch to a custom cell only when the layout goes beyond one simple text block.
Related reading
- How do I write a custom init for a UIView subclass in Swift?
- How do I write a custom init for a UIView subclass in Swift?
- How do I write dispatch_after GCD in Swift 3, 4, and 5?
- How do I write dispatch_after GCD in Swift 3, 4, and 5?
- How do I xcodebuild a static library with Bitcode enabled?
- How do iOS Push Notifications work?
- How do we use runOnUiThread in Android?
- How do you access command line arguments in Swift?
.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.