Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it a way to keep contentOffset after reloading tableview?

I need to reload tableview, but in somehow I want to keep the scroll position. Any way to do it? Maybe store content offset and in viewDidLayoutSubviews I should reset it?

like image 851
János Avatar asked Sep 29 '22 08:09

János


2 Answers

TableView does not reset scroll position. It maintains content offset even after reloadData function is called. So no need to do anything, if you want to keep pixel position.

If you are changing row height at the time of reloading data or changing datasource then you can use scrollToRowAtIndexPath:atScrollPosition:animated: method.

like image 180
Apurv Avatar answered Oct 05 '22 08:10

Apurv


You can save your current offset before reloading the table use this method to save your current offset

self.tableView.contentOffset.y

Or you can Reload like this it will Reload your table from your current Offset Try this:

[UIView animateWithDuration:0.1 delay:0 options:0 animations:^{
     self.tableView.contentOffset = CGPointMake(0, currentOffset);
} completion:^(BOOL finished) {
     [self.tableView reloadData];
}];
like image 40
Chitresh Goyal Avatar answered Oct 05 '22 07:10

Chitresh Goyal