2014-01-26 4 views
1

저는 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

당신의 모든 코드를 보지 않고 말을 어려울 것이다, 그러나 여기 맞춤형 세포에 자동 레이아웃 사용의 좋은 예입니다.

포스트 - 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은 종종 제약 조건에 대한 문제를 의미 할 수 있습니다. 경우에 따라 제약 조건은 높이가 0 일 때만 충족 될 수 있습니다. 프로그래밍 방식으로 또는 IB에서이 작업을 수행 했습니까? HILargeMessageImageCell에 대한 제약 조건을 공유 할 수 있습니까?

+0

인터페이스 빌더 내에서 제약 조건을 수행했습니다. –

+0

셀을 삭제하고 다시 처리하면 모든 것이 다시 작동합니다. 무슨 일이 일어 났는지 누가 알 겠어! –

+0

IB에서 엉망진창이 생기고 셀 높이가 실제로 0 이었음을 기꺼이 확신합니다. – ansible

관련 문제