可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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?
回答1:
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.
回答2:
Have you tried using [view setContentOffset: offset animated: YES]
?
回答3:
This worked nicely for me:
if (self.tableView.isDecelerating) {
NSArray *paths = [self.tableView indexPathsForVisibleRows];
[self.tableView scrollToRowAtIndexPath:[paths objectAtIndex:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
}
回答4:
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!
回答5:
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 *
回答6:
You could wait for the 'decelerating' property to become NO (e.g. by using KVO) and switch after that.
回答7:
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.
回答8:
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
回答9:
You can also do self.tableView.isScrollEnabled = true/false
when you get to a certain tableView.contentOffset.y
value
回答10:
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.