在Swift中使用autolayout时获取错误的tableVIew.contentSize

use*_*970 6 uitableview ios swift

当我使用autolayout自动调整单元格时

我设置这些:

tableView.estimatedRowHeight = 50
tableView.rowHeight = UITableViewAutomaticDimension
Run Code Online (Sandbox Code Playgroud)

但是当我需要获取表格的contentSize.height时

tableView.reloadData()
tableView.layoutIfNeeded()
print(tableView.contentSize.height)
Run Code Online (Sandbox Code Playgroud)

如果tableView中有三个单元格,并且每个高度为20

tableView.contentSize.height会给我150的高度

但真正的contentSize.height是60

我怎么解决这个问题?

Jor*_*n H -1

听起来这可能是一个很好的场景,可以在 contentSize 发生变化时使用 KVO(键值观察)来收到通知。当使用估计高度时,它经常会发生变化,例如当您滚动并显示更多单元格时。