简体   繁体   English

swift scrollToRow 或 setContentOffset 不起作用

[英]swift scrollToRow or setContentOffset doesn't work

I have a tableview.我有一个表格视图。 When I pull up to load more data , as long as the scroll bar is scrolling , the method of scrollToTop will stop the tableview from scrolling instead of scrolling to the top, until the scrolling stops.当我拉起加载更多数据时,只要滚动条在滚动,scrollToTop的方法就会停止tableview滚动而不是滚动到顶部,直到滚动停止。

All other situations can works good.所有其他情况都可以正常工作。

How to solve this problem?如何解决这个问题呢? Thank you for your help.谢谢您的帮助。

scrollToTop funcs scrollToTop 函数

func scrollToTop1(animated: Bool) {
    tableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: .top, animated: animated)
}

func scrollToTop2(animated: Bool) {
    tableView.setContentOffset(CGPoint(x: 0, y: 0), animated: true)
}

Tried the following methods:尝试了以下方法:

  1. reloadData() before scrollToTop scrollToTop 之前的 reloadData()

No effect没有效果


  1. Async delay scrollToTop异步延迟 scrollToTop
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(100)) {
    scrollToTop()
}

No effect没有效果


3.Use beginUpdates() and endUpdates() 3.使用 beginUpdates() 和 endUpdates()

beginUpdates()
scrollToTop()
beginUpdates()

Can roll to the top, but there will be strange flicker可以滚动到顶部,但是会有奇怪的闪烁


  1. Remove scroll animation卸下卷轴 animation
tableView.setContentOffset(CGPoint(x: 0, y: 0), animated: false) 

Can roll to the top, but no animated可以滚动到顶部,但没有动画

The problem has been solved.问题已解决。 Strictly speaking, my code is not rigorous.严格来说,我的代码并不严谨。 I wrote a method to call api to get data.我写了一个方法调用 api 来获取数据。 Both refresh and pull-up load will access the same method.刷新和上拉加载都将访问相同的方法。

For convenience, the method UIRefreshControl.endRefreshing will be executed regardless of the pull-up load or pull-down refresh, which leads to this strange problem.为了方便, UIRefreshControl.endRefreshing方法不管是上拉加载还是下拉刷新都会执行,导致了这个奇怪的问题。

The record is as above, I hope it can help people who also encounter this weird problem记录如上,希望对同样遇到这个奇怪问题的人有所帮助

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

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