4
votes

I try to build a talbleview where a every row can display a sublist when it is tapped. In order to that I update the Datamodel and call

[self.tableView insertRowsAtIndexPaths:addedIndexPaths withRowAnimation:(UITableViewRowAnimationTop)];

The animation takes place while the rows are visible. The added cells display correctly after the animation, but while the animating the tableview sometimes scrolls up. This happens especially in the last section of the tableview. Also the tableview always scrolls up when table until the top of the cell that is above the inserted cells is visible.

I want to fix the contentoffset during the visible animation. I tried to set the context offset inside of begin updates and end updates, but this did not work because the animation would scroll up and you would see the content scrolling back into position.

I use auto layout to make the cells self-sizing.

How can I keep the content offset of the tableview constant while inserting visible cells?

1
You are asking the table view to scroll to the top of the added cell when you specify UITableViewRowAnimationTop. If you don't want this to happen you need to pick a new row animation. You likely want UITableViewRowAnimationNone. All your options are documented here developer.apple.com/library/ios/documentation/UIKit/Reference/…. - Dare
I am not so sure but: It could be that you are using autoLayout but you are not messaging autoLayout to adjust itself after each animation.If that is what you are doing wrong then see this video. Hope it helps. youtube.com/watch?v=N5Ert6LTruY - Honey
@asma22 I do use auto layout to size my cells. i tried and called self.tableview layoutIfNeeded inside of the begin / end updates block but it did not solve the problem. How would you go about informing auto layout? - HasenBaumeister
I'd do exactly you suggested - Honey
@Dare I already tried UITableViewRowAnimationNone. I did not work . In some sections it works just fine. But especially close to the last section of the tableview the jumping starts - HasenBaumeister

1 Answers

0
votes

I figured out the issue. The self-resizing is the root cause.

Don't use self-resizing Just remove these lines:

tableView.estimatedRowHeight = 123

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {