我探索了关于这个问题的现有问答,但没有找到我的答案。
我知道这是由 tableview 在运行时不知道自定义单元格的高度引起的,但不知道如何克服这个问题。这是 iOS 8 + Xcode 6。我为自定义单元格的内在大小执行了所有自动布局所需的方法...
-
带有标准单元格的标准表格视图,只有一个(行 = 2)在代码中创建为自定义单元格;
自定义单元格:
-(CGSize)intrinsicContentSize
{
//用于测试目的的硬编码
return CGSizeMake(500.0, 450.0);
}
在iOS模拟器上运行时,发现customCell显示在其行下方的其他单元格的“下方”,具有标准高度,与其他标准单元格高度的设置方式相同,而不是将其高度设置为比其他单元格大得多。
在表视图控制器中:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell;
pageViewCellTableViewCell* customCell;
if (indexPath.row != 2)
{
cell = [tableView dequeueReusableCellWithIdentifier:@"regularCell" forIndexPath:indexPath];
cell.textLabel.text = [NSString stringWithFormat:@"Table Row %lu", indexPath.row];
return cell;
}
else
{
customCell = [tableView dequeueReusableCellWithIdentifier:@"customCell"] ;
if (customCell == nil) {
customCell = [[customCell alloc] init];
}
customCell.parentViewController = self;
[customCell setNeedsUpdateConstraints];
[customCell setNeedsLayout];
[customCell setNeedsDisplay];
return customCell;
}
return nil ;
}
- (CGFloat)tableView: (UITableView*)tableView EstimatedHeightForRowAtIndexPath: (NSIndexPath*) indexPath
{
if (indexPath.row != 2)
return 10;
else
return 450;
}
- (CGFloat)tableView: (UITableView*)tableView HeightForRowAtIndexPath: (NSIndexPath*) indexPath
{
if (indexPath.row != 2)
return 10;
else
return 450;
}
在模拟器上运行时:
收到以下错误消息:
仅警告一次:检测到约束模糊地建议表视图单元格的内容视图的高度为零的情况。我们认为倒塌是无意的,并使用标准高度。
几天前我遇到了类似的错误。我建议您再次检查自动布局约束。
设置时tableView.rowHeight = UITableViewAutomatic
您必须确保单元格中的每个元素都有前导、顶部和底部约束。否则 swift 无法根据内容大小动态更改高度。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)