【发布时间】:2012-02-20 15:27:13
【问题描述】:
我有一个带有自定义表格视图单元格的表格视图。它们的高度取决于单元格中出现的文本数量。这是我的 UITableViewDelegate 方法 heightForRowAtIndexPath 的代码:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger row = [indexPath row];
NSDictionary *factor = [self.factorsArray objectAtIndex:row];
CGSize maximumLabelSize = CGSizeMake(260, kMaximumFactorLabelHeight);
CGSize labelSize = [[factor objectForKey:@"text"] sizeWithFont:[UIFont fontWithName:@"Arial-BoldMT" size:14.0]
constrainedToSize:maximumLabelSize
lineBreakMode:UILineBreakModeWordWrap];
return (labelSize.height + 72);
}
此代码完美运行。我的问题是这里有很多东西是硬编码的:
- 表格视图单元格中标签的宽度
- 字体名称
- 字体大小
- “72”是除了标签高度之外需要添加到单元格的额外高度。
我可以从包含自定义表格视图单元格的 xib 文件中获取所有这些信息。但是我如何在 heightForRowAtIndexPath: 中得到它?我试过 - (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath 但我遇到了崩溃,我相信是因为调用 heightForRowAtIndexPath 方法时单元格还不存在。
【问题讨论】:
标签: ios uitableview