简体   繁体   English

分组 UITableView scrollToRow 不工作

[英]Grouped UITableView scrollToRow not working

I'm using UITableView(frame: .zero, style: .grouped) with multiple sections and dynamic heights of cells.我正在使用UITableView(frame: .zero, style: .grouped)具有多个部分和单元格的动态高度。 Each section has a header.每个部分都有一个 header。

I'm trying to scroll to the bottom of the table view我正在尝试滚动到表格视图的底部

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

My problem is:我的问题是:

when there is only one item in the section , the table view gets scrolled to the header instead of the item.section 中只有一项时,表格视图将滚动到 header 而不是该项。 When there is more than one item everything works well.当有多个项目时,一切正常。

Additionally for the plain style of table view, everything is ok.此外,对于plain的表格视图样式,一切都很好。

Try using below code,尝试使用下面的代码,

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)
}

It turns out that estimatedHeightForHeaderInSection was not precise enough.事实证明estimatedHeightForHeaderInSection不够精确。 Even though I don't use dynamic heights for the section headers.即使我不使用节标题的动态高度。

So the final fix is: use the same values in the tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) and tableView(_ tableView: UITableView, estimatedHeightForHeaderInSection section: Int)所以最后的解决方法是:在tableView(_ tableView: UITableView, heightForHeaderInSection section: Int)tableView(_ tableView: UITableView, estimatedHeightForHeaderInSection section: Int)中使用相同的值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM