【问题标题】:Resize tableview frame to fit dynamic contents using cell size autolayout使用单元格大小自动布局调整表格视图框架的大小以适应动态内容
【发布时间】:2015-10-13 20:00:06
【问题描述】:

当我调整tableView 的大小时,内容被截断,因为contentSize 似乎是由tableView.estimatedRowHeight = 18 决定的。

tableView 的实际contentSize(针对自动布局进行了调整)比sizeToFit() 认为的要大。

如何告诉sizeToFit() 我的tableView 内容的正确大小?

 tableView.frame = CGRectMake( 0, self.scrollView.frame.size.height + 18, self.view.frame.size.width, 350 )
 tableView.estimatedRowHeight = 18
 tableView.rowHeight = UITableViewAutomaticDimension

 self.view.addSubview(self.tableView)
 tableView.sizeToFit()

【问题讨论】:

    标签: ios swift uitableview autolayout ios9


    【解决方案1】:

    如何告诉 sizeToFit() 我的 tableView 内容的正确大小

    你没有。表视图,作为一个视图,它不知道它有多少行,甚至不知道你认为它应该是它的行的高度。 (它读不懂你的心思。)你必须自己根据行数和它们的高度来计算它的高度。

    【讨论】:

    • 自动布局调整单元格后如何获取单元格大小?
    • 一个单元格是一个视图。看看它的高度。
    • 正确答案是在 viewDidAppear 中获取每个单元格的高度。这将考虑自动布局高度调整。对于 self.tableView.visibleCells 中的单元格 { newTableHeight = newTableHeight + cell.frame.height }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多