UITableView
Swift
iOS development
text wrapping
UITableViewCell

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.

Browse interview questions

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.

swift
1import UIKit
2
3final class NotesViewController: UITableViewController {
4    private let items = [
5        "Short note.",
6        "This is a much longer note that should wrap across multiple lines inside the standard table view cell without creating a custom subclass."
7    ]
8
9    override func viewDidLoad() {
10        super.viewDidLoad()
11        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
12    }
13
14    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
15        items.count
16    }
17
18    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
19        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
20        var content = cell.defaultContentConfiguration()
21        content.text = items[indexPath.row]
22        content.textProperties.numberOfLines = 0
23        cell.contentConfiguration = content
24        return cell
25    }
26}

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.

swift
1override func viewDidLoad() {
2    super.viewDidLoad()
3    tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
4    tableView.rowHeight = UITableView.automaticDimension
5    tableView.estimatedRowHeight = 60
6}

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.

swift
1override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
2    let cell = tableView.dequeueReusableCell(withIdentifier: "LegacyCell")
3        ?? UITableViewCell(style: .default, reuseIdentifier: "LegacyCell")
4
5    cell.textLabel?.text = items[indexPath.row]
6    cell.textLabel?.numberOfLines = 0
7    cell.textLabel?.lineBreakMode = .byWordWrapping
8    return cell
9}

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 = 0 but leaving tableView.rowHeight fixed, 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 sizeToFit on 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 automaticDimension and 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.automaticDimension so row height follows the content.
  • Prefer defaultContentConfiguration() in modern code and textLabel only for older code paths.
  • Switch to a custom cell only when the layout goes beyond one simple text block.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.