Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a multiline table cell in iOS?

How can I get the second cell to expand to fit the text rather than scaling the text? Is there a built in way of doing this in iOS or will I have to come up with some home-cooked solution? If you look in the iOS Contacts application, there's a box like this for Address. I can't find how to implement this though.

Table screenshot


For anyone looking to achieve this in future, here's the code for my solution:

HEADER file:

#define FONT_SIZE 22.0f
#define CELL_CONTENT_WIDTH 320.0f
#define CELL_CONTENT_MARGIN 5.0f

IMPLEMENTATION file:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.section == 0 && indexPath.row == 1) {

        NSString *text = [atmAnnotation address];

        CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);

        CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
        NSLog(@"Size for address is Height:%f Width:%f",size.height,size.width);

        CGFloat height = MAX(size.height, 44.0f);

        return height + (CELL_CONTENT_MARGIN * 2);  

    }
    return 44.0f;
}

Here's a screenshot of the result:

UITableView fixed

like image 846
conorgriffin Avatar asked Feb 09 '11 23:02

conorgriffin


People also ask

How to have multiple lines in Markdown table?

In summary, if you need to have a table cell span multiple lines when writing Markdown, use the HTML <br> tag, as shown.

How do you make a table with multiple lines on long text in CSS?

When the text in a single table cell exceeds a few words, a line break (<BR>) may improve the appearance and readability of the table. The line break code allows data to be split into multiple lines. Place the line break code <BR> within the text at the point(s) you want the line to break.


1 Answers

Unfortunately, you are going to have to implement this feature yourself. There are a variety of methods and callbacks you need to make use of to calculate the height of the rows and labels. If you need some help getting started, I can amend this answer to include some sample code. Otherwise, I'm sure there are some related questions here on SO or Google that can get you started. In summary, however:

  • Determine the height of the row in the UITableViewDelegate method -tableView:heightForRowAtIndexPath:. You'll probably need to use the NSString UIKit Addition methods to calculate how tall your string will be with a given font size, etc. Return the proper height from this delegate method.
  • Make sure your label (either in IB or configured programmatically) is set to use multiple lines. If you set numberOfLines to 0, the label will use as many lines as necessary.
  • In your UITableViewDataSource implementation of -tableView:cellForRowAtIndexPath:, you'll need to use the same logic as before to determine the frame of your labels. Set the text you want, then change the frame so that the label is tall enough to just barely fit all the text.
like image 105
CIFilter Avatar answered Oct 02 '22 13:10

CIFilter