Dhaval Dobariya
Dhaval Dobariya

Reputation: 171

Maintain current position of UITableView while calling reloadData with variable height custom cells of UITableView

I have UITableView with variable height custom cells and multiple sections which are not fixed, i am trying to implement load more functionality while user reach at first cell.

After fetching data i am arranging records into NSMutableArray which contains multi-dimensional array to store data section vice.

My problem is when i load more data i don't have idea about how many sections and how many rows in each section comes. So i can not add fix values to move my UITableView at particular position using methods like scrollToRowAtIndexPath or scrollRectToVisible

So every time after getting new record i called reloadData to update my number Of Sections and number Of Rows In Each Section, which also move control to first row of UITableView. I want to be present at current viewing cell not at first cell.

I have also tried answers at reloadData() of UITableView with Dynamic cell heights causes jumpy scrolling this question but that are not helping me.

Upvotes: 0

Views: 997

Answers (1)

Cœur
Cœur

Reputation: 38667

Don't use reloadData if you want to stay at the same position. Use reloadRowsAtIndexPaths or insertRowsAtIndexPaths or reloadSections instead.

To refresh modified rows with animation:

[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:@[indexPathOfYourModifiedCell] withRowAnimation: UITableViewRowAnimationAutomatic];
[self.tableView endUpdates];

To add rows with animation (number of rows is automatically increased):

[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:@[indexPathOfYourNewCell] withRowAnimation: UITableViewRowAnimationAutomatic];
[self.tableView endUpdates];

Without animation (untested):

[UIView performWithoutAnimation:^{
    [self.tableView beginUpdates];
    [self.tableView insertRowsAtIndexPaths:@[indexPathOfYourNewCell] withRowAnimation:UITableViewRowAnimationNone];
    [self.tableView endUpdates];
}];

Apple documentation: description here

Upvotes: 1

Related Questions