Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add multiple lines to detailTextLabel in UITableViewCell

I'm using a UITableView with cell style of UITableViewCellStyleValue1.

I'm want to have multiple lines in the detailTextLabel, is that possible? Or do I have to make a custom cell?

like image 983
mshaaban Avatar asked Aug 20 '10 23:08

mshaaban


2 Answers

You can set cell.detailTextLabel.numberOfLines = 2 to get 2 lines in there. However, I doubt UITableViewCell will lay out the labels as you expect in that case. You may want to subclass UITableViewCell and override -layoutSubviews to position the labels how you want. You can call [super layoutSubviews] and then just tweak the positions of the labels. You'll probably want to use -[NSString sizeWithFont:constrainedToSize:lineBreakMode:] to calculate the correct size for the detail text label.

Alternatively, instead of subclassing UITableViewCell, you could try doing the tweaks in -tableView:willDisplayCell:forRowAtIndexPath:, though if the cell ever decides it needs to re-layout, then your tweaks will be erased. I recommend you go with the subclassing approach.

Edit: BTW, with the subclass approach, all you have to do is change [UITableViewCell alloc] to [MyTableViewCellSubclass alloc]. Since you're not introducing new methods or properties, the variable can still remain typed as a UITableViewCell and you won't have to change any other code.

like image 161
Lily Ballard Avatar answered Nov 15 '22 21:11

Lily Ballard


The detailTextLabel is a UILabel, the word wrapping rules apply just like with a label you create.

http://developer.apple.com/iphone/library/documentation/uikit/reference/UILabel_Class/Reference/UILabel.html#//apple_ref/occ/instp/UILabel/lineBreakMode

EDIT: Just thought, you'll also need to change the height of the cell if the text gets beyond some threshold of lines. probably 2 or 3 is okay.

like image 43
NWCoder Avatar answered Nov 15 '22 21:11

NWCoder