0

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

Boaz
  • 4,864
  • 12
  • 50
  • 90
  • refer this link...http://stackoverflow.com/questions/24278539/find-out-height-of-a-uilabel-which-is-size-to-fit/24279122#24279122 – karthikeyan Jun 30 '14 at 13:45
  • didn't get how it is different from what I've been doing – Boaz Jun 30 '14 at 13:50
  • Try with removing sizetofit code – Vishnu Jun 30 '14 at 13:54
  • @Vishnu - didn't help. Actually it was left there because I thought it will solve the problem (and didn't remove it since) – Boaz Jun 30 '14 at 14:00
  • You should remove the cell sizing code from cellForRowAtIndexPath, it should only be in heightForRowAtIndexPath. The label's numberOfLines should be set to 0 in IB, or in the cell's init method. Also, you don't need the if ([commentsArray count] == 0) check -- cellForRowAtIndexPath won't be called if commentsArray is empty (assuming you're returning its count in numberOfRowsInSection), and in any case you can't return nil from that method.. – rdelmar Jun 30 '14 at 15:01
  • @rdelmar - setting cell height wasn't enough. The commentText label is not the only control in the cell... – Boaz Jun 30 '14 at 15:03
  • did you override heightForRowAtIndexPath ? – Lena Bru Jul 01 '14 at 08:30
  • @LenaBru - yep... it has rather similar code, and as you can see the cell height changes OK in both cases (and in any case you can see it in the code in the question) – Boaz Jul 01 '14 at 08:33

2 Answers2

1

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

Boaz
  • 4,864
  • 12
  • 50
  • 90
0

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.