【发布时间】:2016-07-30 14:30:23
【问题描述】:
我正在使用 iOS 7 设备创建应用程序。此应用程序包含带有动态单元格高度的UITableView。
要创建它,我有一个包含方法的自定义单元格:
- (CGFloat)heightWithModel:(Model *)model
{
CGFloat height = 0.0f;
height = self.contentLabel.frame.origin.y + [self contentHeightWithModel:model] + CellBottomOffset;
return height;
}
还有……
- (CGFloat)contentHeightWithModel:(Model *)model
{
CGFloat height = 0.0;
NSDictionary *attributes = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14.0f] forKey:NSFontAttributeName];
NSString *string = model.content;
NSStringDrawingContext *context = nil;
NSStringDrawingOptions options = NSStringDrawingUsesLineFragmentOrigin;
CGSize size = CGSizeMake(self.contentLabel.bounds.size.width, CGFLOAT_MAX);
CGRect frame = [string boundingRectWithSize:size options:options attributes:attributes context:context];
height = frame.size.height;
return height;
}
在我的视图控制器中,我实现了UITableViewDelegate协议方法:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat height;
static Cell *cell;
if (!cell)
{
cell = [tableView dequeueReusableCellWithIdentifier:CellReuseIdentifier];
}
height = [cell heightWithModel:[self.dataSource.models objectAtIndex:indexPath.row]];
return height;
}
据我了解,这应该足以创建具有动态单元格高度的表格视图。尽管如此,我有一个像这样的表格视图:
如您所见,部分文本被隐藏,因为标签(红色的)高度太小。使用自动布局(距底部 10 像素)动态设置单元格高度。
谁能看出问题出在哪里?
【问题讨论】:
标签: ios objective-c uitableview