gpt4 book ai didi

ios - 什么时候应该重新计算 UITableView 页脚的大小?

转载 作者:行者123 更新时间:2023-11-28 07:39:29 24 4
gpt4 key购买 nike

我想根据表格 View 不断变化的内容大小重新计算表格 View 页脚的高度。当表格有零行时,页脚的高度将达到最大值。随着行被添加到表中,页脚的高度将降低,直到达到最小值。我正在做的是使用页脚填充零行或几行时出现在表格底部的空白空间。除了添加行之外,内容大小也可能发生变化,因为现有行的高度(内容)已发生变化。

假设我有一个 View Controller ,其主视图包含两个 subview :一个按钮和一个 TableView 。单击该按钮会导致修改数据存储并调用表的 reloadData 方法。我何时/何地为表格的 tableFooterView.bounds.size.height 分配一个新值?

我还应该指出,我正在使用 UITableViewAutomaticDimension。如果在表的数据源委托(delegate)方法 cellForRowAt 中,我打印我得到的单元格高度:

Upper table cell height = 21.0
Upper table cell height = 21.0
Upper table cell height = 21.0
Upper table cell height = 21.0
Upper table cell height = 44.0

所有 21 个,除了最后一个,新的一个。这一定是由于尚未应用自动尺寸标注。

更新:

我已经初步得出了以下解决方案(非常感谢 this 线程上的所有人提供了解决方案的大部分内容)。我是暂时的,因为该解决方案涉及两次调用 reloadData 以处理 contentSize 的问题。参见 this contentSize 问题演示的 GitHub 项目。

class TableView: UITableView {

override func reloadData() {
execute() { super.reloadData() }
}

override func reloadRows(at indexPaths: [IndexPath], with animation: UITableView.RowAnimation) {
execute() { super.reloadRows(at: indexPaths, with: animation) }
}

private func execute(reload: @escaping () -> Void) {
CATransaction.begin()
CATransaction.setCompletionBlock() {
if self.adjustFooter() {
reload() // Cause the contentSize to update (see GitHub project)
self.layoutIfNeeded()
}
}
reload()
CATransaction.commit()
}

// Return true(false) if the footer was(was not) adjusted
func adjustFooter() -> Bool {
guard let currentFrame = tableFooterView?.frame else { return false }

let newHeight = calcFooterHeight()
let adjustmentNeeded = newHeight != currentFrame.height

if adjustmentNeeded {
tableFooterView?.frame = CGRect(x: currentFrame.minX, y: currentFrame.minY, width: currentFrame.width, height: newHeight)
}

return adjustmentNeeded
}

private let minFooterHeight: CGFloat = 44
private func calcFooterHeight() -> CGFloat {
guard let footerView = tableFooterView else { return 0 }

let spaceTaken = contentSize.height - footerView.bounds.height
let spaceAvailable = bounds.height - spaceTaken
return spaceAvailable > minFooterHeight ? spaceAvailable : minFooterHeight
}
}

最佳答案

UITableViewDelegate 有方法 tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat 我们可以用它来指定节页脚的高度。当我们为表格 View 调用 reloadData() 或更改屏幕方向等时,此方法会触发。

因此您可以实现此方法来计算页脚的新高度:

    override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {

guard section == 0 else { return 0.0 } // assume there is only one section in the table

var cellsHeight: CGFloat = 0.0

let rows = self.tableView(tableView, numberOfRowsInSection: section)

for row in 0..<rows
{
let indexPath = IndexPath(item: row, section: section)
cellsHeight += self.tableView(tableView, heightForRowAt: indexPath)
}

let headerHeight: CGFloat = tableView.tableHeaderView?.frame.height ?? 0.0
let footerHeight = view.frame.height - headerHeight - cellsHeight

return footerHeight
}

关于ios - 什么时候应该重新计算 UITableView 页脚的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52648071/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com