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

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

这是我的代码

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

即使 方法 1(调用方法 3)被调用,Table 视图仍会继续其自然滚动减速直到停止。它没有将自己定位在我想要的位置。

当 Table 视图不滚动并且我通过按下按钮调用 方法 2 时,它确实设置了内容偏移量并定位到应有的位置。

如何通过在方法 1 中调用它来使其工作?

谢谢

如果我没看错,下面的方法应该有效:

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

您还应该实施 scrollViewDidEndDecelerating

scrollViewDidEndDragging: willDecelerate:

scrollViewDidEndDragging: willDecelerate: 总是被调用

scrollViewDidEndDecelerating在table减速时调用,减速结束