【问题标题】:UITableviewCell dynamic height issueUITableviewCell 动态高度问题
【发布时间】:2014-12-15 04:54:27
【问题描述】:

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

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

但高度总是返回为 0,谁能帮我纠正这个问题?

你可以在这里看到代码https://github.com/iamabhiee/Github

【问题讨论】:

  • 在您创建约束的地方显示您的单元格 xib 文件或 .m 文件
  • @GrzegorzKrukowski 你可以在这里看到代码github.com/iamabhiee/Github

标签: ios objective-c uitableview ios7 autolayout


【解决方案1】:

使用此代码

    - (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;
}

【讨论】:

  • 谢谢,但我正在寻找更好的解决方案,因为我将拥有多种类型的 tableviewcell
【解决方案2】:

您有错误的约束并且缺少底部约束 + 您缺少 UILabel 的首选宽度。 看看我的屏幕截图 - 在这些修复之后它可以工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-27
    • 1970-01-01
    • 2019-10-12
    • 2011-06-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多