views:

3245

answers:

8

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?

A: 

You could try doing

tableView.scrollEnabled = NO;
tableView.scrollEnabled = YES;

This might stop the scroll by disabling it, then allow it again. I haven't tried this specifically, but I've done similar things.

Dan Lorenc
I tried setting it to NO, then set the contentOffset, then set it back to YES. That didn't work, but I might as well try the sequence NO-YES-offset and see if that makes a difference.
Daniel Dickison
A: 

You could wait for the 'decelerating' property to become NO (e.g. by using KVO) and switch after that.

Nikolai Ruhe
The problem with that is that there would be a rather noticeable delay before the scroll position can be set.
Daniel Dickison
A: 

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

Ben Gottlieb
I'll give it a shot. It's not exactly the behavior I want (I want the scroll position to snap right away), but it might be good enough.
Daniel Dickison
A: 

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.

Daniel Dickison
+4  A: 

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.

Corey Floyd
scrollRectToVisible did it, using the saved scroll position as the rect's origin and using the table view's bounds as the height. You are absolutely correct that the scroll* methods control the acceleration in ways setting the content offset doesn't.
Daniel Dickison
`scrollRectToVisible` worked for me as well.
zekel
+1  A: 

I used Corey's approach. I save & restore the rects with the dictionary representation. Also, it may not be obvious but the rect to preserve & restore is the bounds of the UITableView:

// Save the current tableview bounds
CGRect contentRect = self.listTableView.bounds;
if (!!oldScope) [_contentRects setObject:(NSObject *)CGRectCreateDictionaryRepresentation(contentRect) forKey:oldScope];

// Restore if possible
CFDictionaryRef restoredFrameDict = (CFDictionaryRef)[_contentRects objectForKey:newScope];
if (!restoredFrameDict) restoredFrameDict = CGRectCreateDictionaryRepresentation(CGRectZero);
CGRectMakeWithDictionaryRepresentation(restoredFrameDict, &contentRect);

// Switch over to new datasource
self.listTableView.dataSource = [self dataSourceForScope:newScope];
[self.listTableView reloadData];

// Restore content offset for "newScope"; Also stops scrolling
[self.listTableView scrollRectToVisible:contentRect animated:NO];

Note the interchangeable use of CFDictionaryRef and NSDictionary *

ohhorob
A: 

Just found this looking for a way to stop my UIScrollView - I needed to move some subviews around, but this wound't look right if the user had flicked the screen and it was still decelerating.

Anyway - scrollRectToVisible didn't work for me (maybe because I'm not using a table view??) but this worked perfectly:

[mainScrollView setContentOffset:CGPointMake(mainScrollView.contentOffset.x, mainScrollView.contentOffset.y) animated:NO];

I can then do the subview stuff without worrying!

Ben Robinson
A: 

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:

NSIndexPath *exPath = [[subTable indexPathsForVisibleRows] lastObject];

isMatchOn = [whatList selectedSegmentIndex] == 0 ? YES : NO; //table source will make the choice looking at this BOOL

[subTable reloadData]; // here tables actually flip

if (lastPosition) { [subTable scrollToRowAtIndexPath:lastPosition atScrollPosition:UITableViewScrollPositionBottom animated:NO]; //I scroll to saved index } self.lastPosition = exPath; //here I save the current position

Jim75