2

The sizeToFit method of UILabel doesn't apply in cellForRowAtIndexPath.

The problem is I need some of my cells to look like this: enter image description here

and others to look like this: enter image description here

What's the solution for this?

Thanks

Meet Doshi
  • 3,983
  • 10
  • 35
  • 76
diogo.appDev
  • 1,605
  • 5
  • 16
  • 29

2 Answers2

1

The sizeToFit method does what it is supposed to do in the cellForRowAtIndexPath method after disabling auto layout in the storyboard.

diogo.appDev
  • 1,605
  • 5
  • 16
  • 29
0

I had the same problem and I solved this way:

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIFont *font = [UIFont fontWithName:@"Roboto-Regular" size:14];
    CGSize textSize = [[eventDetails objectForKey:[detailTitle objectAtIndex: indexPath.row]] sizeWithFont:font];
    CGFloat strikeWidth = textSize.width;
    if(strikeWidth <280) // if the length is lower than max width
    {
        return  30; // normal height of cell
    }
    else
    {
        return 30 + ((int) strikeWidth/280 )*15;// if not give the cell the height you want
    }
}

And do not forget to set the number of lines of label to 0 [label setNumberOfLines:0];

Meet Doshi
  • 3,983
  • 10
  • 35
  • 76
Constantin Saulenco
  • 2,185
  • 18
  • 41