1
votes

I'm experiencing some trouble with a UILabel in a UITableViewCell that extend its height nicely. But when the cell is reused, and the UILabel text changes, scrolling back up passed labels with correct height when I scrolled down, now is wrong.

As far as I can understand, this is an issue with the UILabel height, but I don't understand what and why it happens.

Heres a video of it happening:

UITableView

You can see how the labels in the table cells below the images is looking good, but when I scroll back up, they are suddenly only two lines, instead of four.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    InstagramMedia *media = mediaArray[indexPath.section];

    CGSize maximumLabelSize = CGSizeMake(304.0f, 20000.0f);
    CGSize expectedLabelSize = [media.caption.text sizeWithFont:[UIFont systemFontOfSize:13.0f] constrainedToSize:maximumLabelSize lineBreakMode:NSLineBreakByWordWrapping];

    return (320.0f + expectedLabelSize.height + 20.0f);
}

Am I missing something here?

2
Is the label the size it should be or it is smaller than expected? - wootage
@wootage At first its how it should be, but when scrolling up and down, it changes to a wrong size. - rebellion
Have you tried to set different colors to the label and cell background to see the actual sizes?I guess you have custom class for the table cell, implement - (void)layoutSubviews in that class and set the label size there - wootage
@wootage It seems like the cell has the correct height on all cells. It's the UILabel which are making the trouble. It looks like its height is randomly select when scrolling up and down. Some of them are correct when scrolling down, but when I scroll back up, the UILabel height is wrong. - rebellion
Then layoutSubviews in the cell class should do your job.Just set the label frame in it - wootage

2 Answers

2
votes

I have tried using slightly different approach and it worked in the simulator. Cells kept their height on scrolling.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    InstagramMedia *media = mediaArray[indexPath.section];

    NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"HelveticaNeue" size:12], NSFonatAttributeName, nil];
    NSAttributedString *textStr = [[NSAttrebutedString alloc]initWithString:media.caption.text attributes:attributes];
    CGSize maximumLabelSize = CGSizeMake(304.0f, 20000.0f);
    CGRect expLblRect = [textStr boundingRectWithSize:maximumLabelSize options:NSStringDrawingUsesLineFragmentOrigin context:nil];

    return (320.0f + expLblFrame.size.height + 20.0f);
}
2
votes

Try this :

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

    UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 186, 0)]; //max width that is allowed for label
    lbl.numberOfLines = 0;
    lbl.lineBreakMode = NSLineBreakByWordWrapping;

    int GAP = 3;
    float height = 0;
    float fontSize = 14;
    NSString *cellText = yourtext;
    lbl.font= [ UIFont fontWithName:FONT_NAME_Ubuntu size: fontSize];
    bl.text = cellText;
    [lbl sizeToFit];
    height += lbl.frame.size.height;
    height += GAP;

    return height;
  }         

Or you can try this :

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

        /*
        for dynamic cell heigh according to the description display in cell
         */

        NSDictionary *dict1 = (NSDictionary*)[array objectAtIndex:indexPath.row];
        NSString *cellText = [dict1 valueForKey:@"test"];
        UIFont *cellFont = [UIFont fontWithName:FONT_NAME_GOTHAM_4 size:11.0];
        CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
        CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];


            return labelSize.height + 80;


}