Last active
January 4, 2016 06:59
-
-
Save alexshafran/8585597 to your computer and use it in GitHub Desktop.
Reload TableView preserving offset
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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