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?
You could wait for the 'decelerating' property to become NO (e.g. by using KVO) and switch after that.
This worked nicely for me:
You can also do
self.tableView.isScrollEnabled = true/false
when you get to a certaintableView.contentOffset.y
valueHave you tried using
[view setContentOffset: offset animated: YES]
?One obvious work around is to have two separate table views that get swapped out, and each data source is permanently attached to one of the table views. This just seemed like a bit of a waste of memory, but perhaps I'm over-thinking it.
It was my problem when I had a UISwitch as selector for the tables. But with the segmented control I haven't any problem. Maybe you didn't reload the table? This is my piece of working code: