UITableviewCell动态高度问题

时间:2014-10-20 08:50:41

标签: ios objective-c uitableview ios7 autolayout

你好我在tableview单元格中有一个标签,标签的高度根据内容而改变,所以在设置之后尝试按照以下代码计算高度

CGSize cellSize = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
CGFloat height = cellSize.height;

但是高度总是返回为0,任何人都可以帮我纠正这个吗?

您可以在此处查看代码https://github.com/iamabhiee/Github

2 个答案:

答案 0 :(得分:0)

使用此代码

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 
CGFloat width=[UIScreen mainScreen].bounds.size.width;
//width - rigth and left constraints 8
NSString* strText = @"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum";

NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:17] forKey: NSFontAttributeName];

CGRect frame = [strText boundingRectWithSize:CGSizeMake(width - 8 - 8, 3000) options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin attributes:attributesDictionary context:nil];
// 16 for bottom and top space
return frame.size.height + 16;
}

答案 1 :(得分:0)

你有错误的约束和缺少底部约束+你缺少UILabel的首选宽度。 看看我的截图 - 它可以在修复后运行。

fix

相关问题