0

プロトタイプセルを含むテーブルがあります。私のプロジェクトは自動レイアウトを使用しています。

セルにはいくつかのラベルがあり、テキストの長さはさまざまです。長すぎてデフォルトのサイズに収まらない場合があります。

What I'd like to change label/cell size dynamically to enable to show the whole text. If needed, add more rows automatically.

I'd tried label's sizetofit, it simply does not do anything.

4

1 に答える 1

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;
}
于 2013-03-03T17:28:56.680 に答える