Swift动态表格单元格高度

时间:2016-04-12 12:58:53

标签: ios uitableview height ios-autolayout

我有原型细胞的tableview, 在单元格中我有imageview和一些文本。 文本标签是原型单元格中的一个,但有时它不止一行,我在服务器调用后将数据加载到表视图中。 故事板行中的标签设置为0,换行设置为Word Wrap。 我也试过了http://candycode.io/automatically-resizing-uitableviewcells-with-dynamic-text-height-using-auto-layout/

但没有效果。 如果我使用Using Auto Layout in UITableView for dynamic cell layouts & variable row heights 一切正常,因为标签文本是预定义的,但我正在从服务器加载数据,我在API调用后重新加载tableView,然后标签只是一行。

2 个答案:

答案 0 :(得分:30)

您应该使用UITableViewAutomaticDimension提供显示动态内容的解决方案。

viewDidLoad中使用以下代码:

tableView.estimatedRowHeight = YourTableViewHeight
tableView.rowHeight = UITableViewAutomaticDimension

Read more from here

希望它有所帮助。 :)

答案 1 :(得分:18)

确保UILabel高度约束不是常数,如果常量则必须从属性检查器

设置大于或等于

确保根据您的。

设置UITableViewestimate行高
self.TableName.estimatedRowHeight = 85

and write in UITableViewcell class 


 Cell.lbl_name.sizeToFit()


 Cell.lbl_name.numberOfLines = 0

确保您的UITableViewDelegate方法

func tableView(tableView: UITableView,
    heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{

 return UITableViewAutomaticDimension
}