使用iOS 8.3模拟器和Xcode6.3.2。
我在iOS 8中使用了自调整单元格大小的技术,当单元格没有辅助视图时,它的效果非常好,但当单元格有辅助视图时,它就会中断。我在单元格内的标签上设置了约束:

然后我使用estimatedRowHeight并将rowHeight设置为UITableViewAutomaticDimension (我在这里省略了tableView:numberOfRowsInSection:,但在本例中它只返回3):
@implementation MyTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.estimatedRowHeight = 44.0f;
self.tableView.rowHeight = UITableViewAutomaticDimension;
}
...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MyCell *cell = (MyCell *)[tableView dequeueReusableCellWithIdentifier:@"myCell" forIndexPath:indexPath];
cell.titleLabel.text = @"Test comment Test comment Test comment Test comment Test comment Test comment Test comment Test comment Test comment Test comment Test comment ";
return cell;
}
@end然后,一切看起来都很棒:

但是当我在Storyboard中添加一个附件视图时,没有更改任何代码:

第一个细胞会下地狱。Xcode的视图调试器告诉我标签高度是1785磅,我必须使用gif在这里显示它:

我确实注意到其他两个单元格的大小都很好。我还注意到,当我旋转模拟器时,第一个单元格的大小会适当地调整,包括在它旋转回纵向之后。
有人知道为什么附件视图会把事情搞得这么糟吗?我能做些什么来修复它呢?在http://github.com/UberJason/SelfSizingNightmare上提供了一个示例项目。
发布于 2015-07-12 21:10:49
这确实是一个bug。我已经把你的项目发给苹果了。这是来自苹果的回复。

发布于 2015-07-09 21:18:27
这似乎是由细胞内带有numberOfLines = 0的标签引起的。
虽然我不确定为什么,但看起来好像将附件添加到单元格中会使自动布局在表格首次加载时无法计算标签的高度。
我能够通过给标签添加一个首选的最大宽度来修复你的例子--这似乎足以让布局系统及时计算出高度:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MyCell *cell = (MyCell *)[tableView dequeueReusableCellWithIdentifier:@"myCell" forIndexPath:indexPath];
// Give the layout system something to help it estimate the label height
cell.titleLabel.preferredMaxLayoutWidth = self.titleLabel.bounds.size.width;
cell.titleLabel.text = @"Test comment Test comment Test comment Test comment Test comment Test comment Test comment Test comment Test comment Test comment Test comment ";
return cell;
}或者,在viewDidAppear中调用[tableView reloadData]似乎也可以解决这个问题。
https://stackoverflow.com/questions/31056840
复制相似问题