Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: UITableView cells with multiple lines?

Tags:

ios

ipad

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.

thanks

like image 569
aneuryzm Avatar asked May 11 '11 12:05

aneuryzm


2 Answers

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.

like image 141
rekle Avatar answered Sep 23 '22 06:09

rekle


Since Swift 3:

func allowMultipleLines(tableViewCell: UITableViewCell) {     tableViewCell.textLabel?.numberOfLines = 0     tableViewCell.textLabel?.lineBreakMode = .byWordWrapping } 
like image 20
Gobe Avatar answered Sep 23 '22 06:09

Gobe