ios 动态调整单元格大小时如何获取 UITableView 的高度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40079970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get height of UITableView when cells are dynamically sized?
提问by leonardloo
I have a UITableView with cells that are dynamically sized. That means I have set:
我有一个带有动态大小的单元格的 UITableView。这意味着我已经设置:
tableView.estimatedRowHeight = 50.0
tableView.rowHeight = UITableViewAutomaticDimension
Now I want to get the height of the whole table view. I tried getting it through tableView.contentSize.height
but that only returns the estimated row height, and not the actual dynamic height of the table view.
现在我想获得整个表格视图的高度。我尝试通过它tableView.contentSize.height
但只返回估计的行高,而不是表视图的实际动态高度。
How do I get the dynamic height of the whole table view?
如何获取整个表格视图的动态高度?
回答by leonardloo
I finally hacked out a solution:
我终于找到了一个解决方案:
tableView.contentSize.height
will not work for dynamic cells because they will only return the number of cells * estimatedRowHeight
.
tableView.contentSize.height
不适用于动态单元格,因为它们只会返回单元格的数量 * estimatedRowHeight
。
Hence, to get the dynamic table view height, you look for all visible cells, and sum up their heights. Note that this only works for table views that are shorter than your screen.
因此,要获得动态表格视图高度,您需要查找所有可见单元格,并总结它们的高度。请注意,这仅适用于比屏幕短的表格视图。
However, before we do the above to look for visible cells, it is important to know that note we need to get the table view on the screen so that we can obtain visible cells. To do so, we can set a height constraint for the table view to some arbitrarily large number just so it appears on the screen:
但是,在我们执行上述查找可见单元格之前,重要的是要知道注意我们需要在屏幕上获取表格视图,以便我们可以获得可见单元格。为此,我们可以为 table view 设置一个任意大的高度约束,以便它出现在屏幕上:
Set height of table view constraint:
// 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)
Call tableView.layoutIfNeeded(), and when completed, look for the visible cells, sum up their height, and edit the
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 }
设置表格视图约束的高度:
// 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)
调用 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 }
回答by Ebed Kharistian
it's already been answered, but I want to share my experience as well.
它已经得到了回答,但我也想分享我的经验。
I have the same problem. I've searched answers in many similar questions and tried their answers and doesn't work.
我也有同样的问题。我搜索了许多类似问题的答案并尝试了他们的答案,但没有用。
Finally I've found leonardloo answer. Thanks a lot, but it doesn't solved my problem yet. I just want to complete his answer. I put this code from his answer:
最后我找到了 leonardloo answer。非常感谢,但它还没有解决我的问题。我只想完成他的回答。我把这段代码从他的回答里拿出来:
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 }
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 }
in this block of code:
在这段代码中:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
...
if indexPath.row == data.count-1{
// Insert the code here
}
return cell
}
It means after all the rows in table have been loaded, we update the height of the row using that piece of code. It works!!!
这意味着在加载 table 中的所有行之后,我们使用该段代码更新行的高度。 有用!!!
回答by Yusuf Kamil AK
Even though @leonardloo's answer is partially correct and it may suit the needs of many people, I wanted to share how I fixed my issue.
尽管@leonardloo 的答案部分正确并且可能适合许多人的需求,但我还是想分享我如何解决我的问题。
I had a large tableView with big rows and had the same issue. When I've applied @leonardloo's answer, I've seen that it draws only number of visible cells it has found in the beginningand it draws 1 row whereas it's supposed to draw 5 (my datasource count).
我有一个大行的大 tableView 并且遇到了同样的问题。当我应用@leonardloo 的答案时,我看到它只绘制了它在开始时找到的可见单元格的数量,它绘制了 1 行,而它应该绘制 5 个(我的数据源计数)。
To fix that, I have found such a simple solution:
为了解决这个问题,我找到了一个简单的解决方案:
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
// Set tableView's constraint to height + 1 so that
// it can still draw the next visible cell it's supposed to.
self.heightOfTableViewConstraint.constant = heightOfTableView + 1
}
The point here is to let it draw the next cell so that it will be counted in visibleCells as well by setting constraint constant to height + 1. I've tested and it seems working like a charm now.
这里的重点是让它绘制下一个单元格,以便通过将约束常量设置为高度 + 1 来计算它也将在可见单元格中计数。我已经测试过,现在它看起来像一个魅力。
回答by Bharat Modi
You should calculate the height of tableView in the following delegate method,
您应该在以下委托方法中计算 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
}
}
回答by Nikhil Thaware
Just maintain an array of type CGFLoat
and append UITableViewAutomaticDimension
in cellForRowAt
indexPath
delegate of tableview
只是保持型的阵列CGFLoat
,并附加UITableViewAutomaticDimension
在cellForRowAt
indexPath
的代表tableview
var cellHeight = [CGFloat]()
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
cellHeight.append(UITableViewAutomaticDimension)
}
回答by Sylvan D Ash
I'll add my answer here since I struggled with this a bit.
我会在这里添加我的答案,因为我对此有点挣扎。
My table view was using sections that had headers, so just getting the height of the visible cells wasn't working. What I ended up doing was first set a super large height for the tableView
so that the content will all be added. Then on the last cell
of the tableView
, I resize the tableView
to be the same height as its content
.
我的表格视图使用了带有标题的部分,因此仅获取可见单元格的高度是行不通的。我最终做的是首先为 设置一个超大的高度,tableView
以便将内容全部添加。然后在最后cell
的tableView
,我调整tableView
到相同高度的content
。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
... // do your normal cell processing
// determine height and change it
if indexPath.section == (self.sectionTitles.count - 1) && indexPath.row == (sectionData.count - 1) {
UIView.animate(withDuration: 0, animations: {
self.tableView.layoutIfNeeded()
}) { complete in
// Edit heightOfTableViewConstraint's constant to update height of table view
self.tableViewHeightConstraint.constant = self.tableView.contentSize.height
}
}
}
回答by jignesh trivedi
you can try this code
你可以试试这个代码
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}