UITableViewAutomaticDimension错误的UITableViewCell高度 [英] Wrong UITableViewCell height with UITableViewAutomaticDimension
问题描述
我使用2个多线标签制作自定义单元格并将此标签固定到所有边。当在 -tableView:heightForRowAtIndexPath:
iOS> = 8 时,我返回 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?
身高代码:
- (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;
}
结果:
滚动后:
推荐答案
当我没有为iOS 8设置tableView的estimatedRowHeight并且每次设置text的文本时更新preferredMaxLayoutWidth时,问题就消失了多行标签
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
这篇关于UITableViewAutomaticDimension错误的UITableViewCell高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!