views:

362

answers:

2

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?

A: 

In -heightForRowAtIndexPath you have to measure the string at your required font size (using something like – sizeWithFont:constrainedToSize:lineBreakMode:) and return the height you need.

ok, taking a closer look.. You are careful to check if your -dequeueReusableCellWithIdentifier is nil, right? If it is not nil you want to reuse it to get better performance. It will already contain a UILabel. And if each cell is a different height you probably don't want to call [tableView setRowHeight:] each time -cellForRowAtIndexPath is called. In - heightForRowAtIndexPath returning the height of the UILabel could work, else try [myString sizeWithFont: affL.font constrainedToSize:CGSizeMake(affL.frame.size.width, CGFLOAT_MAX)]
I guess I don't need to add a label. See my new code below.
Jens Koehler
A: 

I guess I don't need a label added to cell, because cell contains only one string.

Therefore I changed the code to:

//... heightForRow...

NSString *myText = [myDictionary objectForKey:@"affirmation"];

return [myText sizeWithFont:[UIFont systemFontOfSize:12.0f] constrainedToSize:
CGSizeMake(220.0, CGFLOAT_MAX) /* 220 = width of Cell */ lineBreakMode:UILineBreakModeWordWrap].height;

//... cellForRow...

cell = [tableView dequeueReusableCellWithIdentifier:@"affirmationCell"];
if (cell == nil) 
{
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"affirmationCell"]autorelease];
    cell.accessoryType = UITableViewCellAccessoryNone;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.autoresizingMask = UIViewAutoresizingFlexibleHeight;
}

cell.textLabel.text = [myDictionary objectForKey:@"myAff"];

return cell;

But it doesn't work. What's going wrong?

Jens Koehler
I fixed it! Had to add a default value to heightreturn [...].height + 44.0;
Jens Koehler
Thanks a lot for helping me.
Jens Koehler
Nothing you have after a `return` statement will run. The function or method stops executing when it returns.
Peter Hosey