Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically force a UIScrollView to stop scrolling, for sharing a table view with multiple data sources

Tags:

I have a UITableView whose data source and delegate are switched between a couple of custom data source objects when the user touches a segmented control (think "Top Paid" vs "Top Free" in the app store app).

Each data source object saves its last scroll content offset, and restores it when it becomes the active data source for the table view by doing:

tableView.contentOffset = CGPointMake(0, savedScrollPosition); 

This works well when the user switches the data source when the table is at rest, but if the user hits the segmented control while the table is still moving (i.e. decelerating), the table view continues to decelerate from the old offset, effectively overriding my contentOffset assignment.

Is there a way to force the table view to stop scrolling/decelerating when I set the contentOffset, or another way to make this type of switchable-data-source table view work?

like image 315
Daniel Dickison Avatar asked Jun 12 '09 12:06

Daniel Dickison


People also ask

How do I stop UIScrollView scrolling?

For disabling the same we need to make the “isScrollEnabled” property of our scroll view to false. Copy the below code in your file. import UIKit class ViewController: UIViewController { @IBOutlet var scrollView: UIScrollView! override func viewDidLoad() { super.

Is UITableVIew scrollable?

Smooth Scrolling:- As a result, it affects the smoother scrolling experience. In UITableVIew, Scrolling will be smooth through cell reuse with the help of prepareForReuse() method.


2 Answers

Did you try these 2 methods?

They actually apply to the "scrolling" not just the offset of the content.

[self.tableView  scrollToRowAtIndexPath:savedIndexPath atScrollPosition:UITableViewScrollPositionTop animated:NO]; 

OR:

[self.tableView  scrollRectToVisible:savedFrame animated:NO]; 

They should actually effect the scrolling and by extension the acceleration of the table, not just what is visible on screen.

like image 93
Corey Floyd Avatar answered Oct 15 '22 11:10

Corey Floyd


Have you tried using [view setContentOffset: offset animated: YES]?

like image 28
Ben Gottlieb Avatar answered Oct 15 '22 09:10

Ben Gottlieb