Skip to content

Instantly share code, notes, and snippets.

@alexshafran
Last active January 4, 2016 06:59
Show Gist options
  • Save alexshafran/8585597 to your computer and use it in GitHub Desktop.
Save alexshafran/8585597 to your computer and use it in GitHub Desktop.
Reload TableView preserving offset
@implementation UITableView (PreserveOffset)
- (void)reloadDataPreservingOffset
{
CGSize contentSize = self.contentSize;
CGPoint contentOffset = self.contentOffset;
[self reloadData];
CGSize newContentSize = self.contentSize;
CGPoint newContentOffset = self.contentOffset;
if (!CGPointEqualToPoint(contentOffset, newContentOffset)) // content has been inserted above
{
CGFloat adjustedOffsetY = contentOffset.y + newContentSize.height - contentSize.height;
self.contentOffset = CGPointMake(0, adjustedOffsetY);
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment