1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Multiple text lines in a UITableView

Discussion in 'iPhone/iPad Programming' started by beachdog, Sep 1, 2008.

  1. macrumors member

    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?
  2. macrumors 6502a

    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.
  3. macrumors 68030


    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.
  4. macrumors 6502


    Have you got the solution?if,Please let me know
  5. macrumors 6502

    Multiline text in UITableViewCell

    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.

    Attached Files:

  6. macrumors 6502


    Ya suji.I want like this
  7. macrumors regular

  8. macrumors 6502

    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.
  9. macrumors 6502


    I need to display the rss fields in tableview ,So i cant use your above technique
  10. Guest


    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

  11. Moderator


    Staff Member

    Because rss data is different than other kinds of text? :confused:
  12. macrumors member

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

Share This Page