我在SO上查看了现有的关于这个问题的问答,但没有找到我的答案。
我知道这是由于tableview在运行时不知道自定义单元格的高度引起的,但不确定如何克服这个问题。这是iOS 8 + Xcode 6。我已经完成了自定义单元格内在大小所需的所有自动布局方法...
Standard tableview with standard cells, only one (row = 2) created in code as custom cell;
customCell:
-(CGSize)intrinsicContentSize
{ //hardcode for testing purposes
return CGSizeMake(500.0, 450.0);
}
when running on iOS simulator, found that the customCell is displayed "underneath" other cells below its row, with height standard, the same way other standard cell height is set, instead of its height set much bigger than other cells.
在表格视图控制器中:
- (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;
}
当在模拟器上运行时:
收到以下错误消息: 仅警告一次: 检测到约束模棱两可,建议将表格视图单元格的内容视图高度设置为零。我们认为这是意外的折叠,因此使用标准高度。