切换选项卡时,我需要导航到tableview的顶部。
func scrollToFirstRow() {
if let rowCount = self.dataSource?.tableView(self,numberOfRowsInSection: 0), rowCount > 0 {
let indexPath = IndexPath(row: 0, section: 0)
self.scrollToRow(at: indexPath, at: .top, animated: true)
self.setContentOffset(CGPoint(x: 0, y: 10), animated: true)
}
}使用Xcode10.1和iOS11,我可以导航到顶部,同时使用上面的代码切换标签。
但在iOS 13和Xcode11.3测试版中,tableview无法滚动到顶部
发布于 2019-12-11 19:52:59
将indexPath值指定为行0和部分0。这在我的iOS 13中运行得很好。
self.chatTable.scrollToRow(at: IndexPath(row: 0, section: 0), at: .top, animated: true)更新
你需要在tableview加载完成后调用它。下面是如何检查加载结束的方法:
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if let lastVisibleIndexPath = tableView.indexPathsForVisibleRows?.last {
if indexPath == lastVisibleIndexPath {
// call your scrollToFirstRow function here
}
}
}同样在您的viewWillAppear中,只需调用tableView.reloadData()
发布于 2019-12-11 21:17:06
只需调用此方法:
self.chatTable.setContentOffset(CGPointZero, animated:true)
或
self.chatTable.setContentOffset(.zero, animated:true)
发布于 2019-12-12 19:23:12
通过在DispatchQueue上调用scrollToFirstRow()解决
https://stackoverflow.com/questions/59285006
复制相似问题