How to change rowHeight of a TableViewCell depending on Stringlength

Posted by Jens Koehler on Stack Overflow See other posts from Stack Overflow or by Jens Koehler
Published on 2010-02-24T09:46:01Z Indexed on 2010/04/10 1:03 UTC
Read the original article Hit count: 412

I have a table with different entries, each with different length. The height of each row should fit, so it can show the whole string.

I use the following code:

//... cellForRowAtIndexPath
if (row == 0) {

    cell = [tableView dequeueReusableCellWithIdentifier:@"affCell"];
    //if (!cell) {
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"affCell"]autorelease];
     cell.accessoryType = UITableViewCellAccessoryNone;
     cell.selectionStyle = UITableViewCellSelectionStyleNone;
     cell.autoresizingMask = UIViewAutoresizingFlexibleHeight;

     affL = [[UILabel alloc] initWithFrame:CGRectZero];
     affL.textAlignment = UITextAlignmentLeft;
     affL.autoresizingMask = UIViewAutoresizingFlexibleHeight;
     [cell.contentView addSubview:affL];
    //}
    //declaration label
    affL.text = [fDictionary objectForKey:@"aff"];
    affL.numberOfLines = 0;
    [affL sizeToFit];
    affL.frame = CGRectMake(15.0, 10.0, 220.0, affL.frame.size.height);
    CGFloat affLH = affL.frame.size.height;
    [tableView setRowHeight:affLH+30];

I also use

//... heightForRowAtIndexPath

return affL.frame.size.height;

How can I fix this problem?

© Stack Overflow or respective owner

Related posts about uitableviewcell

Related posts about iphone