1

I'm using this popular approach for autoresizing content in UITableViewCell. And it's not working. All I get is this:

enter image description here

My app is a universal app and I'm using iOS 5 with a storyboard. Here's my code for .h file:

@interface SecondMasterViewController : UITableViewController

And for these two methods:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *identifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
        cell.textLabel.numberOfLines = 0;
        cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:17.0];
    }
    cell.textLabel.text = [self.mainCategories objectAtIndex:indexPath.row];
    UIImage *myAwesomeCellImage;
    myAwesomeCellImage = [UIImage imageNamed:
                          [NSString stringWithFormat:@"%@%@",
                           cell.textLabel.text,@".png"]];
    cell.imageView.image = myAwesomeCellImage;

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellText = @"Go get some text for your cell.";
    UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:17.0];
    CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
    CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];

    return labelSize.height + 20;
}

What have I done wrong? I've tried different approaches and all of them don't work.

Thanks in advance!

Community
  • 1
  • 1
Sergey Grischyov
  • 11,729
  • 18
  • 76
  • 117

1 Answers1

2

Do you use the literal string @"Go get some text for your cell." in your -tableView:heightForRowAtIndexPath:? I think you're supposed to replace that with the actual text of the cell, so you can size it out and see how tall the cell needs to be. Try replacing that string with [self.mainCategories objectAtIndex:indexPath.row] (what you set for your cell text) and see if it helps.

Tim
  • 57,767
  • 18
  • 155
  • 161