Boaz
Boaz

Reputation: 5084

UILabel height change in UITableViewCell is only happenning in second scroll

The weirdest thing...

I have a UILabel in a UITableViewCell which changes height automatically according to text content. I managed to have it working - but only on the second time I scroll. When I scroll down, the text is truncated in 1 line. When I scroll up and than down the cell is perfect.

How come?

Code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CommentCell *cell = (CommentCell *)[tableView dequeueReusableCellWithIdentifier:@"CommentCell"];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CommentCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    if ([commentsArray count] == 0)
    {
        return nil;
    }

    CommentDataItem *commentItem = [commentsArray objectAtIndex:indexPath.row];

    [cell.commentText setNumberOfLines:0];
    [cell.commentText setText:commentItem.text];
    NSDictionary *attributes = @{NSFontAttributeName: cell.commentText.font};
    CGRect rect = [cell.commentText.text boundingRectWithSize:CGSizeMake(cell.commentText.frame.size.width, MAXFLOAT)
                                            options:NSStringDrawingUsesLineFragmentOrigin
                                              attributes:attributes
                                            context:nil];
    CGRect labelFrame = cell.commentText.frame;
    labelFrame.size.height = rect.size.height;
    cell.commentText.frame = labelFrame;
    [cell.commentText sizeToFit];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CommentDataItem *commentItem = [commentsArray objectAtIndex:indexPath.row];
    NSString *commentText = commentItem.text;

    NSDictionary *attributes = @{NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:17]};
    CGRect rect = [commentText boundingRectWithSize:CGSizeMake(267, MAXFLOAT)
                                              options:NSStringDrawingUsesLineFragmentOrigin
                                           attributes:attributes
                                              context:nil];
    return rect.size.height + 50;
}

Screenshots to explain the situation:
on the first scroll down this is what I see:
http://imgur.com/IFufIjR,utPdU0d

When I scroll back up and than down, this is what I get (notice that some of the comments now have more than one line as it should be)
http://imgur.com/IFufIjR,utPdU0d#1

Upvotes: 0

Views: 807

Answers (2)

Shehzad Bilal
Shehzad Bilal

Reputation: 144

I was facing the same problem. The reason was the width of uitableview in uiviewcontroller's xib and width of contentview in uitableviewcell's xib were not same.

Upvotes: 0

Boaz
Boaz

Reputation: 5084

Apparently, although I thought I checked it - the cell did have autolayout (in the xib file) - after removing it. Everything is great!

Leaving the question here if someone will encounter the same issue

Upvotes: 1

Related Questions