PDA

View Full Version : Multiple text lines in a UITableView




beachdog
Sep 1, 2008, 09:34 PM
I've done some basic programming of UITableViews, but now I have a need to create a table view with cells that contain multiple lines of text, preferably where each line may have a different Font size. I need to create something that looks like the table view in the "Inbox" view of the mail application. Can someone give me some pointers on this?



Luke Redpath
Sep 2, 2008, 04:20 AM
The first basic approach would be to create a UILabel for each individual text component needed and add these as subviews of the UITableViewCell, positioned appropriately.

You might find after doing this that your code is cleaner if you extract this into your own custom UITableViewCell subclass (especially if you intend to use it in more than one place).

The iPhone developer docs has a whole section on customizing table cells in the Table view programming guide.

PhoneyDeveloper
Sep 2, 2008, 04:35 PM
If each line in each cell can have a different font then you need a separate Label for each line. If each cell has multiple lines but each cell has only a single font then you can use a single Label in each cell. You don't actually say if all the cells have the same number of lines or if they vary. If they are all the same then just look at the docs and examples. There are several that show how to have multiple lines with different font characteristics.

If you have a variable number of lines you will need to use variable row heights. There is a tableview delegate callback, heightForRowAtIndexPath, that allows you to tell the table the row height for each cell. You will need to calculate the rowheight using NSString:sizeWithFont:constrainedToSize:lineBreakMode:, which isn't as much fun as it sounds. Unfortunately I couldn't figure out a way to determine the width of the cell for certain. What I do is get the width of the screen (320 or 480 depending on orientation) and subtract a factor that I determined emperically from that width. I have a label in my cell so my width won't work for you. Try different widths until you get one that gives you the correct height for your text.

iphonejudy
Mar 12, 2009, 04:58 AM
I've done some basic programming of UITableViews, but now I have a need to create a table view with cells that contain multiple lines of text, preferably where each line may have a different Font size. I need to create something that looks like the table view in the "Inbox" view of the mail application. Can someone give me some pointers on this?

Have you got the solution?if,Please let me know

sujithkrishnan
Mar 12, 2009, 06:17 AM
Are u trying to do something like in the given attached file.If so then create custom view,in draw rect of it draw string.And cell height should be the height of string that u want to display.Custom view should be added as subView to cell.contentView.If u are trying to do something like this,then i can give u code.

iphonejudy
Mar 12, 2009, 06:40 AM
Are u trying to do something like in the given attached file.If so then create custom view,in draw rect of it draw string.And cell height should be the height of string that u want to display.Custom view should be added as subView to cell.contentView.If u are trying to do something like this,then i can give u code.

Ya suji.I want like this

fenrus110
Mar 12, 2009, 12:12 PM
it's just customizing the UITableViewCell. There are a lot of examples of how to do this around already.

http://blog.atrexis.com/index.cfm/2009/1/6/iPhone--Customize-the-UITableCellView
http://iphone.zcentric.com/2008/08/05/custom-uitableviewcell/

North Bronson
Mar 12, 2009, 01:06 PM
Unfortunately I couldn't figure out a way to determine the width of the cell for certain. What I do is get the width of the screen (320 or 480 depending on orientation) and subtract a factor that I determined emperically from that width.

I had this problem and I ended up using this:


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *selectedCustomCell = (CustomCell *)[tableView cellForRowAtIndexPath: indexPath];

UIView *selectedContentView = [selectedCustomCell contentView];

CGRect selectedBounds = selectedContentView.bounds;

NSLog (@"Origin x: %@: selectedBounds.origin.x);

NSLog (@"Origin y: %@: selectedBounds.origin.y);

NSLog (@"Size width: %@: selectedBounds.size.width);

NSLog (@"Size height: %@: selectedBounds.size.height);

// More code
}

This should pop out the dimensions of any cell you touch. You can see that if you have an accessory (like a check-mark or a chevron) the size adjusts accordingly.

iphonejudy
Mar 13, 2009, 01:18 AM
I need to display the rss fields in tableview ,So i cant use your above technique

caveman_uk
Mar 13, 2009, 03:32 AM
Use this method from NSString to get the height of a string

- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(UILineBreakMode)lineBreakMode

None of the others (AFAIK) actually do wrapping except this one

Link (http://developer.apple.com/iphone/library/documentation/UIKit/Reference/NSString_UIKit_Additions/Reference/Reference.html#//apple_ref/occ/instm/NSString/sizeWithFont:constrainedToSize:lineBreakMode:)

dejo
Mar 13, 2009, 10:34 AM
I need to display the rss fields in tableview ,So i cant use your above technique
Because rss data is different than other kinds of text? :confused:

acjrocks
May 11, 2009, 11:13 PM
Are u trying to do something like in the given attached file.If so then create custom view,in draw rect of it draw string.And cell height should be the height of string that u want to display.Custom view should be added as subView to cell.contentView.If u are trying to do something like this,then i can give u code.

Still have the source for this? I sure could use this. Still learning about custom table views and cells.