iOS8中自定义UITableViewCell动态高度的内容具有不同的宽度
问题描述:
我将xcode升级到6.3后遇到此问题。使用xcode 6.2编译的前一版本没有这个问题,并且效果很好。iOS8中自定义UITableViewCell动态高度的内容具有不同的宽度
问题是:切换到其他选项卡后,或从推继续回来后,大多数单元格的宽度会缩小一点点。但有些不。它看起来像以下
单元格第二行中的标签内容具有正确的宽度。所有单元格在首次启动后都会以正确的宽度显示内容。 在上图中,有两个uitableviewcells。 内容视图:蓝色; 背景视图:淡绿色; 内容标签:紫色;
第1个单元格中的bg视图和contentLabel缩小。这就是我不想要的。
我使用故事板来设置约束。 bg视图的约束。 labelview的约束条件
我也尝试缓存单元格高度根据这个问题UITableView layout messing up on push segue and return. (iOS 8, Xcode beta 5, Swift),但它不起作用。
//In viewDidLoad
self.tableView.estimatedRowHeight = 100.0;
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.contentInset = UIEdgeInsetsZero;
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
float height = [[self.estimatedRowHeightCache objectForKey:[NSString stringWithFormat:@"%ld", indexPath.row]] floatValue];
if (height) {
return height;
} else {
return UITableViewAutomaticDimension;
}
}
//cache row height. I observed this doesn't been called in first launching in debugging.
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
[self.estimatedRowHeightCache setValue:[NSNumber numberWithFloat:cell.frame.size.height] forKey:[NSString stringWithFormat:@"%ld", indexPath.row]];
}
感谢您的帮助!
答
那么你没有设置水平空间约束为您的contentView
顶部的视图。你只为你的标签和超视图设置水平空间限制。
做这个
- 选择包含标签
- 编辑器视图>针>前导空格,以上海华
答
实现DataSource的方法泰伯维WillDisplayCell
也可以在细胞类实现(如果它的自定义单元格)方法 - (无效)awakeFromNib有设置单元格边框。
你可以设置背景色的单元格的内容查看?或者设置单元格的背景颜色,或者你已经设置好并检查了? –
我为每个视图设置了背景颜色。 contentView:蓝色;背景视图:淡绿色,这是添加到contentView中的视图作为标签背景。 contentLabel:紫色。 – AngleZhou
你能分享你的代码,你设置这些颜色?另外,你用xib吗? –