Autolayout细胞高度不适用于单细胞

时间:2014-01-26 19:48:24

标签: ios objective-c ios7 autolayout

我一直在使用autolayout来确定其中一个表格中的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 个答案:

答案 0 :(得分:1)

如果没有看到所有代码,很难说,但这是在自定义单元格上使用自动布局的一个很好的例子。

帖子 - https://stackoverflow.com/questions/18746929/using-auto-layout-in-uitableview-for-dynamic-cell-layouts-variable-row-heights

github示例代码 - https://github.com/caoimghgin/TableViewCellWithAutoLayout

此处作者建议首先调用setNeedsUpdateConstraintsupdateConstraintsIfNeeded,如下所示:

// 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的约束吗?