Patrick Patrick - 4 months ago 11
iOS Question

iOS: UITableView cells with multiple lines?

What's the best way to have UITableView cells with multiple lines ? Let's say 5.. or 6 ?

Instead of textLabel and la detailTextLabel ? Should I create a custom style ? or a custom view ?

Any tutorial / example is well accepted.



You can use the existing UILabel views in the UITableViewCell for this. The secret is to do the following:

cell.textLabel.numberOfLines = 0;
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;

By default, the UILabel only allows 1 line of text. Setting numberOfLines to 0 basically removes any limitations on the number of lines displayed. This allows you to have multiple lines of text.

The setting of the lineBreakMode to Word Wrap tells it to word wrap long lines of text onto the next line in the label. If you don't want this, you can skip that line.

You may also have to adjust the height of the table view cell as needed to make more room for the multiple lines of text you add.

For iOS 6.0 and later, use NSLineBreakByWordWrapping instead of UILineBreakModeWordWrap, which has been deprecated.