0

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.

Community
  • 1
  • 1
Dhaval Dobariya
  • 171
  • 1
  • 12

1 Answers1

1

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

dahiya_boy
  • 9,298
  • 1
  • 30
  • 51
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • i think you don't understand my question you are missing something, without calling `reloadData` how you can update your tableview `numberOfRows` and `numberOfSection` ? – Dhaval Dobariya Dec 31 '16 at 05:52
  • @DhavalDobariya I've just changed `reloadRowsAtIndexPaths` with `insertRowsAtIndexPaths`. It does the job correctly and it's documented by Apple in the given link. – Cœur Dec 31 '16 at 06:00
  • @DhavalDobariya Here code does not reload your `tableView`. It just adding one more section and row in your `tableView` or even you can reload a single particular section/cell instead of reloading complete `tableView`. – dahiya_boy Dec 31 '16 at 06:13
  • @Cœur Did you try with dynamic height cells which have no limit about its height. Because i face problem in that case only. And i also try again with `beginUpdates, endUpdates, insertRowsAtIndexPaths and insertSections` methods. But still i have same problem. – Dhaval Dobariya Dec 31 '16 at 06:17
  • @DhavalDobariya Personally I use storyboards prototype cells, autolayout and Swift. And to trigger automatic row height, I use `tableView.estimatedRowHeight = 60` in `viewDidLoad`. This code trick with the value `60` seems important, as nothing else was working for me. – Cœur Dec 31 '16 at 06:26
  • @Cœur I also used same things, the only difference is that i used .xib files and Objective-C but that does't make any difference. In my case `tableView.estimatedRowHeight` is the main problem which i can't set accurately because some of my cell has 60 size or some may have 150 or 480. Because it is messaging screen. – Dhaval Dobariya Dec 31 '16 at 06:32
  • @Cœur i have tried 60 for dynamic value, but still not getting desired output. – Dhaval Dobariya Jan 02 '17 at 04:45