2013-03-03 2 views
0

프로토 타입 셀이있는 테이블이 있습니다. 내 프로젝트는 자동 레이아웃을 사용합니다.라벨 및 셀 크기를 텍스트 길이에 따라 동적으로 변경합니다.

셀에는 일부 레이블이 있으며 텍스트 길이는 다양 할 수 있습니다. 때로는 기본 크기에 맞지 않는 길이입니다.

레이블/셀 크기를 동적으로 변경하여 전체 텍스트를 표시하고 싶습니다. 필요한 경우 자동으로 행을 추가하십시오.

나는 레이블의 sizetofit을 시도했지만 단순히 아무 것도하지 않습니다.

답변

2

다음은 해결책입니다.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *cellText; 
    CGRect screenBound = [[UIScreen mainScreen] bounds]; 
    CGSize screenSize = screenBound.size; 
    CGFloat screenWidth = screenSize.width; 
    cellText = [detailPeriodsContent objectAtIndex:indexPath.row]; 
    UIFont *cellFont = [UIFont systemFontOfSize:14]; 
    CGSize constraintSize = CGSizeMake(screenWidth-40, MAXFLOAT); 
    CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap]; 

    return labelSize.height + 35; 
} 
관련 문제