自动布局单元格高度不适用于单个单元格
Posted
技术标签:
【中文标题】自动布局单元格高度不适用于单个单元格【英文标题】:Autolayout cell height not working for single cell 【发布时间】:2014-01-26 19:48:24 【问题描述】:我一直在使用自动布局来确定我的一张表中的heightForRowAtIndexPath
。它适用于我所有的细胞,除了一个。有什么想法我应该寻找解决问题的方法吗?
HILargeMessageImageCell
没有正确返回高度。现在它返回 0。这是代码:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
UITableViewCell * cell;
if( indexPath.row == 0 )
if( self.message.image )
cell = [tableView dequeueReusableCellWithIdentifier:@"HILargeMessageImageCell"];
[(HILargeMessageImageCell *)cell initWithMessage:self.message];
else
cell = [tableView dequeueReusableCellWithIdentifier:@"HILargeMessageCell"];
[(HILargeMessageCell *)cell initWithMessage:self.message];
else
cell = [tableView dequeueReusableCellWithIdentifier:@"HIChildMessageCell"];
[(HIChildMessageCell *)cell initWithMessage:[self.comments objectAtIndex:indexPath.row-1]];
cell.bounds = CGRectMake(0.0f,0.0f, CGRectGetWidth(self.tableView.bounds), CGRectGetHeight(cell.bounds));
[cell setNeedsLayout];
[cell layoutIfNeeded];
CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
NSLog(@"HEight is: %f",height);
return height + 1.0f; // Add 1.0 for the cell spacer
【问题讨论】:
【参考方案1】:如果没有看到您的所有代码就很难说,但这里是在自定义单元格上使用自动布局的一个很好的例子。
帖子 - https://***.com/questions/18746929/using-auto-layout-in-uitableview-for-dynamic-cell-layouts-variable-row-heights
github 示例代码 - https://github.com/caoimghgin/TableViewCellWithAutoLayout
这里作者建议先调用setNeedsUpdateConstraints
和updateConstraintsIfNeeded
,像这样:
// Make sure the constraints have been added to this cell, since it may have just been created from scratch
[cell setNeedsUpdateConstraints];
[cell updateConstraintsIfNeeded];
// Set the width of the cell to match the width of the table view. This is important so that we'll get the
// correct height for different table view widths, since our cell's height depends on its width due to
// the multi-line UILabel word wrapping. Don't need to do this above in -[tableView:cellForRowAtIndexPath]
// because it happens automatically when the cell is used in the table view.
cell.bounds = CGRectMake(0.0f,0.0f, CGRectGetWidth(self.tableView.bounds), CGRectGetHeight(cell.bounds));
// Do the layout pass on the cell, which will calculate the frames for all the views based on the constraints
// (Note that the preferredMaxLayoutWidth is set on multi-line UILabels inside the -[layoutSubviews] method
// in the UITableViewCell subclass
[cell setNeedsLayout];
[cell layoutIfNeeded];
// Get the actual height required for the cell
CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
// Add an extra point to the height to account for the cell separator, which is added between the bottom
// of the cell's contentView and the bottom of the table view cell.
height += 1;
return height;
编辑:另一个想法 - 0 的高度通常意味着您的约束存在问题。在某些情况下,您的约束只能满足零高度。您是通过编程方式还是在 IB 中执行此操作?你能分享一下HILargeMessageImageCell
的限制条件吗?
【讨论】:
我在界面生成器中做了约束 我删除了单元格并重做了它,一切都恢复了。谁知道发生了什么! 我敢打赌你的约束在 IB 中搞砸了,你的单元格高度实际上是 0。以上是关于自动布局单元格高度不适用于单个单元格的主要内容,如果未能解决你的问题,请参考以下文章