Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrong UITableViewCell height with UITableViewAutomaticDimension

I make custom cell with 2 multiline labels and pin this labels to all sides. When in -tableView:heightForRowAtIndexPath: for iOS >= 8 I return UITableViewAutomaticDimension. But when table view appears the cell height is bigger than should be. After I scroll down and then scroll up the cell takes the normal height. How to fix this behavior?

Code for height:

   - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8) {
        return UITableViewAutomaticDimension;
    }

    static CCTopicTableViewCell *cell;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        cell = [tableView dequeueReusableCellWithIdentifier:@"topicCellIdentifier"];
    });
    cell.topic = [self.topics topicAtIndexPath:indexPath];

    cell.bounds = CGRectMake(0, 0, CGRectGetWidth(self.tableView.bounds), CGRectGetHeight(cell.bounds));

    [cell setNeedsLayout];
    [cell layoutIfNeeded];

    CGSize size = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
    return size.height + 1;
}

And the result:

After scrolling:

like image 229
rabbitinspace Avatar asked Feb 07 '15 14:02

rabbitinspace


1 Answers

The problem is gone when I don't set tableView's estimatedRowHeight for iOS 8 and update preferredMaxLayoutWidth every time when sets the text for multiline labels

like image 123
rabbitinspace Avatar answered Oct 04 '22 07:10

rabbitinspace