我尝试在自定义 uitableviewcell 中使用自动布局
并尝试根据这个SO主题实现动态高度
在 UITableView 中使用自动布局来实现动态单元格布局和可变行高 https://stackoverflow.com/questions/18746929/using-auto-layout-in-uitableview-for-dynamic-cell-layouts-variable-row-heights
但我的单元格是由 xib 文件创建的。所以方法有些不同
这是我在 heightForRowAtIndexPath 中的代码
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
myDataItem *item= [[data items] objectAtIndex:[indexPath row]];
CustomCell *cell = [self.offscreenCells objectForKey:@"CustomCell"];
if (!cell) {
cell = [CustomCell alloc] init];
[self.offscreenCells setObject:cell forKey:@"CustomCell"];
}
[cell.commentView setText:item.comment];
[cell.displayNameView setText:item.displayNameOfItemOwner];
[cell.timeAgoView setText:item.timeAgo];
[cell.imageView setImage:[UIImage imageNamed:@"sis_profile_placeholder"]];
cell.bounds = CGRectMake(0.0f, 0.0f, CGRectGetWidth(tableView.bounds), CGRectGetHeight(_prototypeCell.bounds));
[cell setNeedsLayout];
[cell layoutIfNeeded];
CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
return height+1; }
我从 [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize] 获得的高度始终为“nil”
”
当我尝试调试时,我发现自定义单元格的所有子视图都是“nil”,但单元格本身不是“nil”。这可能是因为我的 customCell 是由 nib 制作的,当我执行 [[CustomCell alloc] init] 时,单元格尚未完全创建。
但我确实尝试将方法更改为 cell = [_tableView dequeueReusableCellWithIdentifier:@"CustomCell"];
结果还是一样。每个子视图都是 nil 。这使得高度返回零并且使我的 tableView 显示不正确。有什么办法可以解决这个问题吗?
我尝试在 AutoLayout 上制作这些。实际上,在以前的版本中,我没有使用 AutoLayout 并手动计算单元格高度。它确实工作得很好。无论如何,我只是想尝试一下 AutoLayout。
请帮忙。提前致谢