1

I have used UITableView to display items from database and I have created custom UITableViewCell which has a few labels. My largest label is name Description.
Each cell can have different height but I can't figure out how to set dynamic cell height.
All I get is three dots at the end of the label and height for cell is always about 50.
How to make cell height based on label inside it?

#define FONT_SIZE 14.0f
#define CELL_CONTENT_WIDTH 320.0f
#define CELL_CONTENT_MARGIN 10.0f

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

    NSString *text = _orderProperty.Description;

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

    CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE]
                   constrainedToSize:constraint
                       lineBreakMode:NSLineBreakByWordWrapping];

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

    return height + (CELL_CONTENT_MARGIN * 2);
}

UPDATE

static NSString *CellIdentifier = @"orderDetailCell";

    OrderDetailCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(cell == nil){
        [tableView registerNib:[UINib nibWithNibName:@"OrderDetailCustomCell" bundle:nil] forCellReuseIdentifier:CellIdentifier];
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    }

    OrderProperty* item = [_list objectAtIndex:indexPath.row];

    cell.Title.text =  item.Title;

    NSString* _description = item.Description;

    cell.Description.text = _description;
Kanan Vora
  • 2,124
  • 1
  • 16
  • 26
1110
  • 7,829
  • 55
  • 176
  • 334
  • see this link user "2014" answer http://stackoverflow.com/questions/17358322/tableview-convertpoint-fromview-odd-behaviour-and-workaround/17359673#17359673 – Darshan Kunjadiya Jul 21 '14 at 13:58

3 Answers3

0

You can try something like this

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    int rowHeight =0.0f;
    NSString *stringToSize=[NSString stringWithFormat:@"%@",longStringValue];
    CGSize size = [stringToSize
                   sizeWithFont:[UIFont systemFontOfSize:15.0f]
                   constrainedToSize:CGSizeMake(300, 6000)
                   lineBreakMode:NSLineBreakByWordWrapping];
    rowHeight = size.height+10;
    return rowHeight;
}

you may get the longStringValue from the array that you are using as a dataSource for your table

modusCell
  • 13,151
  • 9
  • 53
  • 80
Geet
  • 2,427
  • 2
  • 21
  • 39
0

sizeWithFont is now deprecated, please use the method below to set the height of the sell according the the text within the label.

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

   OrderProperty* item = [_list objectAtIndex:indexPath.row]; 

    NSString *text = item.Description;

    NSDictionary *stringAttributes = [NSDictionary dictionaryWithObject:[UIFont fontWithName:@"Helvetica" size:13] forKey:NSFontAttributeName];

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

    CGSize expectedLabelSize = [content boundingRectWithSize: constraint options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin attributes:stringAttributes context:nil].size;

    return expectedLabelSize.height+CELL_CONTENT_MARGIN * 2);
}

Obviously you will need to adjust the string attributes to whatever you are using. These are simply for calculating the required height of the cell.

Jim Tierney
  • 4,078
  • 3
  • 27
  • 48
  • It seams that this is close to solution. When I load view with table some long string cell are truncated. But after scroll down and up it fixes to correct height. Why this could happen? – 1110 Jul 22 '14 at 07:35
  • Can you put up what you have within your cellForRowAtIndexPath: method? Normally to retrieve the text in that cell you would get the text based on the indexPath. Your cellForRow details may help me see if this is the case – Jim Tierney Jul 22 '14 at 08:40
  • OK, thanks for that. If you could add the line OrderProperty* item = [_list objectAtIndex:index path] - this will mean the cell is picking up the correct text for the correct cell. Let me know if it works now. Cheers – Jim Tierney Jul 22 '14 at 09:11
0

You can use following category on NSString, which use NSTextContainer and NSTextContainer to calculate the exact size of your text.

NSString+Calculation.m

And your final code will be like this:

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

    NSString *text = _orderProperty.Description;

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

    CGSize size = [text usedSizeForMaxWidth:constraint.width 
                                   withFont:[UIFont systemFontOfSize:FONT_SIZE]];

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

    return height + (CELL_CONTENT_MARGIN * 2);
}
arturdev
  • 10,884
  • 2
  • 39
  • 67