Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi-line UITableViewCell using UITableViewCellStyleValue2 style

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.

like image 809
petert Avatar asked Dec 11 '09 15:12

petert


3 Answers

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.

like image 64
petert Avatar answered Oct 21 '22 17:10

petert


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.

like image 33
Jasarien Avatar answered Oct 21 '22 19:10

Jasarien


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

like image 1
Andy Avatar answered Oct 21 '22 17:10

Andy