【发布时间】:2012-01-24 00:10:55
【问题描述】:
我有以下设置:具有自定义 UITableViewCell 实现的 UITableView。在每个单元格中,我都有一个 UILabel 和一个 UIButton。首次显示表格时,在每个单元格中,UILabel 的行数设置为 1,并且所有单元格的高度都是固定的(在我的情况下为 60 像素)。当点击单元格中的按钮时,UILabel 的行数设置为 0 并开启自动换行,有效扩展表格单元格。
现在,问题是只有 UITableViewCell 的实现才知道标签是否展开 - 以及单元格高度应该是多少。但是所有者文件是表的数据源。
我不知道如何设置它。任何想法都表示赞赏。
更新这里是我的代码摘录
在自定义 UITableViewCell 实现中:
- (float)requiredHeight
{
if(isFull)
{
CGSize labelSize = [LblTitle.text sizeWithFont: [LblContent font]
constrainedToSize: CGSizeMake(300.0f, 300.0f)
lineBreakMode: UILineBreakModeTailTruncation];
return 42.0f + labelSize.height;
}
else
{
return 60.0f;
}
}
在所有者文件(UITableViewDelegate)中:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
OOCommentCell *cell = (OOCommentCell*)[tableView cellForRowAtIndexPath:indexPath];
return [cell requiredHeight];
}
但是,这会随机导致无限循环或 BAD_ACCESS_EXCEPTION。
【问题讨论】:
标签: iphone dynamic uitableview height