简体   繁体   English

设置UITableView的内容偏移量在滚动时不起作用

[英]Setting content offset of a UITableView not working when it's scrolling

Here's my code 这是我的代码

// 1
func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) {
  scrollTableView()
}

// 2
@IBAction func buttonPressed() {
  scrollTableView()
}

// 3
func scrollTableView() {
  tableView.setContentOffset(CGPointZero, animated: false)
  // tableView.scrollRectToVisible(CGRect(x: 0, y: 0, width: 1, height: 1), animated:true) // does not work either
}

Even method 1 (who calls method 3) being called, the Table View continues its natural scrolling decelerating until it stops. 即使调用了方法1 (调用方法3),“表视图”也会继续自然滚动减速,直到停止。 It does not position itself where I want. 它不会将自己定位在我想要的位置。

When the Table View is not scrolling and I call method 2 by pressing a button it does get its content offset set and it positions where it should. 当“表格视图”不滚动时,我通过按一个按钮来调用方法2 ,它的确设置了内容偏移量,并将其放置在应有的位置。

How can I get it to work by being called in the method 1 ? 如何通过在方法1中被调用来使其工作?

Thanks 谢谢

if i got you right the following should work: 如果我说对了,以下方法应该起作用:

func scrollViewWillBeginDecelerating(scrollView: UIScrollView) {
  scrollTableView()
}

You should also implement scrollViewDidEndDecelerating 您还应该实现scrollViewDidEndDecelerating

with scrollViewDidEndDragging: willDecelerate: scrollViewDidEndDragging: willDecelerate:

scrollViewDidEndDragging: willDecelerate: is called always scrollViewDidEndDragging: willDecelerate:总是被调用

and

scrollViewDidEndDecelerating is called when table decelerate and decelerating is ended. 当表减速和减速结束时,将调用scrollViewDidEndDecelerating

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

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