2011-11-18 5 views
1

heightForRowAtIndexPath 대리자 메서드에서 detailTextLabel 높이를 반환하려고합니다. 이 방법으로 내 테이블 뷰 셀은 내 detailTextLabel과 동일한 높이 여야합니다.올바른 높이 UITableViewCell 설정

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 

    NSString *text = cell.detailTextLabel.text; 
    CGSize size = cell.detailTextLabel.bounds.size; 
    UIFont *font = cell.detailTextLabel.font; 

    CGSize labelSize = [text sizeWithFont:font       
         constrainedToSize:size 
          lineBreakMode:UILineBreakModeWordWrap]; 
    return labelSize.height; 
} 

그러나 나는 다음 줄에 EXC_BAD_ACCESS을 받고 있어요 : 위의 코드 조각 문제

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 

WATS를?

+0

'-tableView : cellForRowAtIndexPath :'중에'-tableView : heightForRowAtIndexPath :'가 호출되므로'-cellForRowAtIndexPath :'를 호출하면 셀이 아직 완전히 생성되지 않아 참조 할 수 없습니다. –

+0

[tableView의 cell.textLabel.text에 액세스하는 방법 : heightForRowAtIndexPath : tableView : cellForRowAtIndexPath :] (http://stackoverflow.com/questions/3903136/how-to-access-cell-textlabel-text-in) 탭에서 열어서 표출하는 경로와 탭에서의 경로) – jrturton

답변

2

cellForRowAtIndexPath 전에 heightForRowAtIndexPath이 호출되는 것이 문제입니다. 그래서 셀은 아직 만들어지지 않았습니다.

모든 행의 높이가 동일한 경우 테이블의 공통 높이를 지정할 수 있습니다. viewDidLoad에서는 self.tableView.rowHeight = ABC;이라고 말할 수 있습니다.이 인터페이스 빌더에서 설정할 수도 있습니다.

2

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 

UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath]; 

해야하지만이 tableView:cellForRowAtIndexPath: 여러 통화, 더 나은 방법으로 발생합니다 :에서 호출, 각 indexPath의 높이를 계산하는 도우미 메서드를 쓰기 tableView:heightForRowAtIndexPath:tableView:cellForRowAtIndexPath:은 키를 저장하여 indexpathes가있는 사전에 높이를 저장하고 indexPath가없는 경우 (또는 다시 계산해야하는 경우) 높이만 계산합니다. 높이를 반환합니다.