当单元格dynamicresize时如何获得UITableView的高度?

我有一个UITableViewdynamic大小的单元格。 这意味着我已经设置:

tableView.estimatedRowHeight = 50.0 tableView.rowHeight = UITableViewAutomaticDimension 

现在我想要获得整个表格视图的高度。 我试图通过tableView.contentSize.height得到它,但只返回估计的行高,而不是表视图的实际dynamic高度。

我如何获得整个表视图的dynamic高度?

我终于破解了一个解决scheme:

tableView.contentSize.height不适用于dynamic单元格,因为它们只会返回单元格数量estimatedRowHeight

因此,要获得dynamic表视图高度,请查找所有可见单元格,并总结其高度。 请注意,这只适用于比屏幕短的表格视图。

但是,在我们完成上述操作以查找可见单元格之前,了解该笔记需要在屏幕上获取表格视图以便获取可见单元格很重要 。 为此,我们可以将表视图的高度约束设置为一些任意大的数字,以使其出现在屏幕上:

  1. 设置表视图约束的高度:

     // Class variable heightOfTableViewConstraint set to 1000 heightOfTableViewConstraint = NSLayoutConstraint(item: self.tableView, attribute: .height, relatedBy: .equal, toItem: containerView, attribute: .height, multiplier: 0.0, constant: 1000) containerView.addConstraint(heightOfTableViewConstraint) 
  2. 调用tableView.layoutIfNeeded(),当完成时,寻找可见的单元格,总结它们的高度,并编辑heightOfTableViewConstraint

     UIView.animate(withDuration: 0, animations: { self.tableView.layoutIfNeeded() }) { (complete) in var heightOfTableView: CGFloat = 0.0 // Get visible cells and sum up their heights let cells = self.tableView.visibleCells for cell in cells { heightOfTableView += cell.frame.height } // Edit heightOfTableViewConstraint's constant to update height of table view self.heightOfTableViewConstraint.constant = heightOfTableView } 

你应该在下面的委托方法中计算tableView的高度,

 func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { //Get the height required for the TableView to show all cells if indexPath.row() == (tableView.indexPathsForVisibleRows.last! as! NSIndexPath).row { //End of loading all Visible cells float heightRequiredForTable = tableView.contentSize.height //If cell's are more than 10 or so that it could not fit on the tableView's visible area then you have to go for other way to check for last cell loaded } } 

它已经被回答了,但我也想分享我的经验。

我也有同样的问题。 我已经在许多类似的问题中search了答案,并尝试了他们的答案,不起作用。

最后我find了leonardloo的答案 。 非常感谢,但它并没有解决我的问题呢。 我只想完成他的答案。 我从他的答案中提出这个代码:

 UIView.animate(withDuration: 0, animations: { self.tableView.layoutIfNeeded() }) { (complete) in var heightOfTableView: CGFloat = 0.0 // Get visible cells and sum up their heights let cells = self.tableView.visibleCells for cell in cells { heightOfTableView += cell.frame.height } // Edit heightOfTableViewConstraint's constant to update height of table view self.heightOfTableViewConstraint.constant = heightOfTableView } 

在这块代码中:

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { ... if indexPath.row == data.count-1{ // Insert the code here } return cell } 

这意味着毕竟表中的行已经被加载,我们使用这段代码来更新行的高度。 有用!!!

你可以试试这个代码

 func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { return UITableViewAutomaticDimension } func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { return UITableViewAutomaticDimension } 

只需维护一个CGFLoattypes的数组,并在indexPath cellForRowAt indexPath委托中追加UITableViewAutomaticDimension

 var cellHeight = [CGFloat]() func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { cellHeight.append(UITableViewAutomaticDimension) }