【发布时间】:2012-04-07 07:12:30
【问题描述】:
我需要能够调整 UITableView 中单个单元格的高度,以使其适合其详细标签中的文本量。
我玩过以下游戏,但对我不起作用:
How do I wrap text in a UITableViewCell without a custom cell
尝试的代码:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = 0;
cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:17.0];
}
和
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellText = @"Go get some text for your cell.";
UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:17.0];
CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
return labelSize.height + 20;
}
这不起作用,它在单元格上显示整个字符串,但单元格高度根本不受影响。
【问题讨论】:
-
提供你目前尝试过的代码。
-
添加代码测试失败。
标签: ios objective-c iphone uitableview