我需要能够调整 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;
}
这不起作用,它在单元格上显示整个字符串,但单元格高度根本不受影响。
最佳答案
很简单,只需将其添加到您的代码中即可:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewAutomaticDimension;
}
它会自动计算行高,然后返回一个 float ... :-)
希望这会有所帮助!
关于ios - 根据文本量更改 UITableViewCell 高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9827126/