3

I am trying to reload a particular row from the tableview but the contentOffset of the tableview is reset to (0, 0) after the reload. I tried to add [tableview beginUpdates] and [tableview endUpdates] around the reloadRowsAtIndexPaths but didn't change the behavior.

The question was asked over here Calling reloadRowsAtIndexPaths removes tableView contentOffset but it did not solve the problem. I am pretty sure that animation has nothing to do with this behavior. I am not sure how to maintain the content Offset of the tableview while still reloading the tableview row.

[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

Community
  • 1
  • 1
Jay
  • 31
  • 1
  • 5
  • have you tried this function: scrollToRowAtIndexPath:atScrollPosition:animated after reloading the row? – Javier Flores Font Mar 11 '15 at 23:38
  • Yes, I have tried `scrollToRowAtIndexPath:atScrollPosition:animated` but it did not work. I tried to set the contentOffset after reloading the row but the tableview jumps the offset, which is not the behavior I want. I don't want the tableview content offset to be changed when a row from that tableview is reloaded. – Jay Mar 11 '15 at 23:58
  • Does anyone have any opinions on this question? I am still trying to figure out how to fix this and there is not a lot of information on it. – Jay Mar 31 '15 at 20:06
  • Look this example: http://photocity.googlecode.com/svn/trunk/PhotoCity_iPhone/co/mcolloquy%202/Mobile/Views/UITableViewAdditions.m It's pretty much what you are doing...and it calls [self setContentOffset:contentOffset animated:NO]; – Javier Flores Font Mar 31 '15 at 20:44
  • Also, [tableview beginUpdates] and [tableview endUpdates] should be called when calling deleteRowsAtIndexPaths or insertRowsAtIndexPath, not with reloadRowsAtIndexPaths. Hope it helps. – Javier Flores Font Mar 31 '15 at 20:47

2 Answers2

7

I'm guessing you've implemented tableView:estimatedHeightForRowAtIndexPath: and have it returning 0.

After reloadRowsAtIndexPaths:withRowAnimation:, the tableView determines its offset using the estimated heights given in tableView:estimatedHeightForRowAtIndexPath:. So unless the value you return there is accurate, implementing it will cause your tableView's offset to change after the reload. I had a similar problem and fixed it by removing my implementation of tableView:estimatedHeightForRowAtIndexPath:.

eyuelt
  • 1,386
  • 1
  • 15
  • 22
2

Updating the estimatedRowHeight and estimatedSectionHeaderHeight with more accurate values can also help alleviate this issue if you haven't implemented any of the estimated height delegate methods.

You can use the view inspector to find the heights of your cells and headers.

hhanesand
  • 990
  • 11
  • 28