分组的 UITableView scrollToRow 不工作

Grouped UITableView scrollToRow not working

我正在使用 UITableView(frame: .zero, style: .grouped) 多个部分和单元格的动态高度。每个部分都有一个 header.

我正在尝试滚动到 table 视图的底部

let lastSection = tableView.numberOfSections - 1
let lastIndexPath = IndexPath(row: tableView.numberOfRows(inSection: lastSection) - 1, section: lastSection)
tableView.scrollToRow(at: lastIndexPath, at: .bottom, animated: true)

我的问题是:

当部分 中只有一项时 ,table 视图将滚动到 header 而不是该项。当有多个项目时,一切正常。

另外 table 视图的 plain 样式,一切正常。

尝试使用下面的代码,

DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { 
    let lastSection = tableView.numberOfSections - 1
    let lastIndexPath = IndexPath(row: tableView.numberOfRows(inSection: lastSection) - 1, section: lastSection)
    tableView.scrollToRow(at: lastIndexPath, at: .bottom, animated: true)
}

原来estimatedHeightForHeaderInSection不够精确。尽管我没有为 headers.

部分使用动态高度

所以最后的解决方法是:在 tableView(_ tableView: UITableView, heightForHeaderInSection section: Int)tableView(_ tableView: UITableView, estimatedHeightForHeaderInSection section: Int)

中使用相同的值