2012-11-04 2 views
2

그래서 어떻게 되나요? 표준 UITableViewCells (스타일 - UITableViewCellStyleSubtitle) 콘텐츠로 UITableView (그룹화 된 스타일, 1 섹션)를 구현하고 있습니다. cellForRowAtIndexPath : 다음과 같이 : heightForRowAtIndexPath :NSString sizeWithFont : constrainedToSize : lineBreakMode : 실제 레이블 높이와 다릅니다

cell.textLabel.lineBreakMode = UILineBreakModeWordWrap; 
cell.textLabel.numberOfLines = 0; 
cell.textLabel.font = [UIFont boldSystemFontOfSize:CELL_FONT_SIZE]; 
cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap; 
cell.detailTextLabel.numberOfLines = 0; 
[self configureCell:cell atIndexPath:indexPath]; 

내가있는 tableView 구현 한 셀 높이의 크기를 조정하려면

Test *test = (Test *)[self.frc objectAtIndexPath:indexPath]; 
NSString *cellText = test.test_name; 

UIFont *cellFont = [UIFont boldSystemFontOfSize:CELL_FONT_SIZE]; 

CGFloat horizontalConstraint; 
if (self.bagdePresent) 
{ 
    // there will be big badge! 
    horizontalConstraint = 250.0f; 
} else 
{ 
    // there will NO badge 
    horizontalConstraint = 280.0f; 
} 

CGSize constraintSize = CGSizeMake(horizontalConstraint, MAXFLOAT); 
CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap]; 

// return label height, 20 as gap, 30 for rating image, 
return labelSize.height + 20 + 30; 
내 textLabel.text 내가있는 TableView 일부 PARAMS을 설정 그 이유는, 정말 긴 될 수 있습니다

먼저 셀에 배지 이미지가 있는지 확인합니다. 그렇다면 사용 가능한 가로 공간을 줄입니다.

일부 문자열 (일부 문자열 길이)의 경우 문제가 있습니다. sizeWithFont : constrainedToSize : lineBreakMode : text 속성을 같은 문자열로 설정하면 실제 셀의 textLabel 높이보다 작은 값을 반환합니다. 결과적으로 일부 셀의 셀 경계보다 큰 textLabel이있는 경우 잘못 표시된 문자열에서 \ 단어를 \ 제거하면 sizeWithFont : constrainedToSize : lineBreakMode : 나중에 실제 높이로 textLabel을 확인하는 정확한 높이를 제공합니다.

글꼴에 문제가있을 수 있다고 생각했지만 글꼴은 동일합니다. 여기에서 파는 곳? ;)

UPD1 : 여기가 heightForRowAtIndexPath에서 계산했던 것보다 textLabel라는 키가되는 예입니다 : tall textLabel

Normal textLabel with longer text

답변

2

나는 sizeToFit을 활용하는 것이 좋습니다. 먼저 라벨의 너비를 horizontalConstraint으로 설정 한 다음 [cell.textLabel sizeToFit]으로 전화를 겁니다 (레이블의 높이를 콘텐츠에 맞게 조정하지만 너비를 그대로두고 설정하면 거의 좁아 질 수 있음).

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    CGFloat horizontalConstraint; 
    if (self.bagdePresent) 
    { 
     // there will be big badge! 
     horizontalConstraint = 250.0f; 
    } else 
    { 
     // there will NO badge 
     horizontalConstraint = 280.0f; 
    } 

    Test *test = (Test *)[self.frc objectAtIndexPath:indexPath]; 
    NSString *cellText = test.test_name; 

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero]; 
    label.text = test.test_name; 
    UIFont *cellFont = [UIFont boldSystemFontOfSize:CELL_FONT_SIZE];   
    label.lineBreakMode = UILineBreakModeWordWrap; 
    label.numberOfLines = 0; 
    label.font = cellFont; 

    CGRect frame = label.frame; 
    frame.size.width = horizontalConstraint; 
    label.frame = frame; 
    [label sizeToFit]; 

    CGSize labelSize = label.frame.size; 

    // return label height, 20 as gap, 30 for rating image, 
    return labelSize.height + 20 + 30; 
} 
+0

도비는 제안을 주셔서 감사합니다,하지만 전화 cellForRowAtIndexPath : heightForRowAtIndexPath에서는 : 무한 루프를 만듭니다 내가 ... 내가 생각하고 그것을 반환하기 전에,있는 TableView는 높이를하고 싶어, 셀을 얻으려고 heightForRowAtIndexPath에서 셀을 가져 오는 또 다른 방법입니다. 그러나 해결책을 찾지 못했습니다. 가능하다고 생각하십니까? – Arseniy

+1

내 대답을 편집했습니다. 새 UILabel을 만들고 특성을 설정하고 그것에서 높이를 얻으십시오. – Tobi

+0

불행히도 그것은하지 않았다. 일하고, 세포는 전에와 같이 행동한다. 재미있는 점은이 문제를 일으키는 약간의 텍스트 길이가 있다는 것입니다. 사진에서 볼 수있는 것은 셀 텍스트에 몇 가지 문자를 추가하면 해당 문자가 OK로 렌더링되기 시작합니다 (일부 문자를 제거 할 때도 마찬가지입니다). 다음에 어디로 가야할지 모르겠다. – Arseniy

관련 문제