views:

992

answers:

4

I'm trying to figure out how to replicate the UITableViewCellStyleValue2 style so that the detail text can be multiple lines - as seen in the 'address' cells in the Contacts app. Like the Contacts app, some of the fields (like street name) are optional; so it would show say 3 lines instead of 4, if the street was not nil.

I'm I missing a trick, or do I have to create a custom cell in IB? How to ensure the text and detail text labels line-up with other UITableViewCellStyleValue2 cells?

Thanks for any tips.

+1  A: 

It sounds to me like you'll have to create a custom UITableCell. The only way to ensure the text lines up is to get the margin/text width values correct, which can be done via trial and error, or using a measuring tool such as xScope.

Jasarien
+2  A: 

Another round of searching found this:

http://the-lost-beauty.blogspot.com/2009/11/multi-line-uitableviewcell-using.html

Quickly tried it, and it works - just need to set the font size down a bit.

petert
A: 

I also had the problem that the textLabel and the detailTextLabel had a different position. Solution: For the detailTextLabel use the same height like the textLabel (e.g. 13)

testing
A: 

Create a custom cell for you table and place a UILabel and a UITextView inside it. Position the label & text view to match their x,y positions to the other cells you are using in that table. You insert "\n" in the textview's text wherever you want line breaks to occur. You resize the textview height depending on the number of lines in the textview using something like:

CGRect frame = yourTextView.frame; frame.size.height = yourTextView.contentSize.height; yourTextView.frame = frame; return frame.size.height + 20.0; // Pad the cell's height as necessary for your applicaion

Andy