简体   繁体   中英

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. 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.

How can I get it to work by being called in the method 1 ?

Thanks

if i got you right the following should work:

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

You should also implement scrollViewDidEndDecelerating

with scrollViewDidEndDragging: willDecelerate:

scrollViewDidEndDragging: willDecelerate: is called always

and

scrollViewDidEndDecelerating is called when table decelerate and decelerating is ended.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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