Skip to content

Instantly share code, notes, and snippets.

@kolinkrewinkel
Created September 20, 2012 00:48
Show Gist options
  • Save kolinkrewinkel/3753301 to your computer and use it in GitHub Desktop.
Save kolinkrewinkel/3753301 to your computer and use it in GitHub Desktop.
Maintaining UITableView Scroll Position
NSIndexPath *zeroPath = [self.tableView.indexPathsForVisibleRows objectAtIndex:0];
STRPost *zeroPost = [self.fetchedResultsController objectAtIndexPath:zeroPath];
CGRect originalPosition = [self.tableView rectForRowAtIndexPath:zeroPath];
CGPoint oldContentOffset = self.tableView.contentOffset;
[self.managedObjectContext mergeChangesFromContextDidSaveNotification:note];
NSIndexPath *newIndexPath = [self.fetchedResultsController indexPathForObject:zeroPost];
CGRect newPosition = [self.tableView rectForRowAtIndexPath:newIndexPath];
CGFloat offsetPosition = originalPosition.origin.y - oldContentOffset.y;
self.tableView.contentOffset = CGPointMake(0.f, (newPosition.origin.y - offsetPosition) + self.tableView.tableHeaderView.frame.size.height);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment