tags:

views:

37

answers:

2

Hello,

I'm using a UITableView with cell style of UITableViewCellStyleValue1. I'm want to have multiple lines in the detailTextLabel, is that possible? and how? Or do I have to make a custom cell.

Any help is appreciated. Thanks, Mohsen

+1  A: 

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.

NWCoder
+1  A: 

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.

Kevin Ballard
Thanks so much for the reply. I subclassed UITableViewCell but I created a nib to layout the labels instead of doing it programmatically. Would that be any different than what you are suggesting me doing. Thanks anyway.
mshaaban
Creating a nib is perfectly fine (though if you're on iOS 4.0 I recommend using `UINib` to optimize the nib loading). The benefit to tweaking the labels using `-layoutSubviews` is you can reuse the existing `textLabel` and `detailTextLabel` properties, but you are by no means required to use those.
Kevin Ballard