2012-01-14 4 views
0

남자. 다른 셀을 가진 UITableView가 있고 높이를 계산하는 코드가 있습니다. 한 프로젝트에서 완벽하게 작동하지만 초당 높이가 0을 반환합니다. 이 문제의 원인은 무엇입니까? 내 코드 : heightForRowAtIndexPath: 위임 방법은 cellForRowAtIndexPath: 대리자 메서드 전에 호출되는신비한 트러블에 크기가 붙어 있습니다

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath]; 
    CGFloat cellWidth = 320.0f; 
    CGSize size = [cell.textLabel.text sizeWithFont:cell.textLabel.font constrainedToSize:CGSizeMake(cellWidth, CGFLOAT_MAX) lineBreakMode:cell.textLabel.lineBreakMode]; 
    CGFloat height = size.height; 
    NSLog(@"Height: %f", height); 
    return height; 
} 

답변

3

. 코드에서 cell.textLabel.text를 기반으로 셀 높이를 계산하지만 셀의 텍스트가 아직 설정되지 않았습니다.

이 방법으로 사용할 텍스트를 표 셀 이외의 다른 곳에서 가져와야합니다 (cellForRowAtIndexPath에 textlabel.text 값을 설정할 때 어디에서 가져올 수 있을지도 모름).

+0

정답은 +1입니다. 먼저 heightForRowAtIndexPath가 호출되고 cellForRowAtIndexPath가 호출됩니다. 왜 OP가 나에게 미스테리가되기 전에 이것이 작동하는지 보았습니다. – Till

+0

문제는 더 힘들다고 생각합니다. – werbary

관련 문제