2014-01-26 184 views
1

我一直在使用自動佈局來確定我的一個表格中的heightForRowAtIndexPath。它適用於除我之外的所有細胞。任何想法我應該尋找解決問題?Autolayout單元格高度不適用於單個單元格

這是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://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; 

編輯:另一個想法 - 的HIGHT 0通常意味着你的約束有問題。在某些情況下,您的限制只能滿足零高度。你是以編程方式還是在IB中執行此操作?你能分享你對HILargeMessageImageCell的限制嗎?

+0

我在界面生成器中做了約束 –

+0

我刪除了單元並重新編譯它,並且所有工作都重新開始。誰知道發生了什麼事! –

+0

我敢打賭,你的約束在IB中搞砸了,你的細胞高度實際上是0. – ansible

相關問題