0

内容に基づいて、テーブルビューのセルに必要な高さを計算しようとしています。これは にあり- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPathます。

ただし、sizeWithFont が 0 を返す場合があります。1 つを除いてすべてのパラメーターがハードコードされているため、これがどのように可能であるかがわかりません。そのパラメーターは変更されていないことがわかります。

NSString *address = [properties objectAtIndex:1];

CGFloat calculatedSize = [address sizeWithFont:[[UIFont alloc] fontWithSize:15.f] constrainedToSize:CGSizeMake(207.f, 100.f) lineBreakMode:UILineBreakModeWordWrap].height;

NSLog(@"Calculated size for %@: %f", address, calculatedSize);

calculatedSize += 19.f;

return calculatedSize;

これは、ビューを何度か行ったり来たりして、これを数回試した場合の出力です。

Calculated size for 1234 Fake St: 0.000000
Calculated size for 1234 Fake St: 0.000000
Calculated size for 1234 Fake St: 0.000000
Calculated size for 1234 Fake St: 38.000000
Calculated size for 1234 Fake St: 0.000000
Calculated size for 1234 Fake St: 38.000000
4

2 に答える 2

3

交換:

[[UIFont alloc] fontWithSize:15.0f]

と:

[UIFont systemFontOfSize:15.f]
于 2012-06-25T09:03:18.947 に答える
0

私はこれをテストしましたが、うまくいきました

NSString *address = @"Sample text Sample text Sample text Sample text Sample text Sample text Sample text";

CGFloat calculatedSize = [address sizeWithFont:[UIFont systemFontOfSize:15.0] 
                             constrainedToSize:CGSizeMake(207.f, 100.f) 
                                 lineBreakMode:UILineBreakModeWordWrap].height;    

NSLog(@"Calculated size for %@: %f", address, calculatedSize); //String... 76.000000
于 2012-06-25T09:06:04.833 に答える