1
votes

I try to implement a tableView where rows additional rows are inserted with an animation while the rows are visible. I update the array for the datasource and call:

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

I want the tableview to not scroll at all and the rows to be inserted on screen (no over the visible are). In some section especially the last section of the tableview the cells are inserted the tableview scrolls to completely different section.

I tried different row animation, calling layoutIfNeeded inside of and beginUpdate / endUpdate block, set the correct content offset inside of the beginUpdate / endUpdate block or reload the entire section.

Nothing works. The scrollview always scrolls up.

The cells size them selves using autolayout-constraints. On the top half of the tableview the cells are inserted as I expected.

How can I fix the content Offset while cells are inserted with an animation? How can I debug that animation?

2
Did you try other UITableViewRowAnimation options. Please include relevant code.. - KrishnaCA
Yes I tried all other row animations. It did not make a difference. From what I know this is all the code you need (if the data model was updated) to kick of the animated insertion. And in most cases it works just fine, but not always.. - paul

2 Answers

1
votes

I faced same issue and after trying a lot found solution that if I remove section header height as UITableViewAutomaticDimension then adding and removing rows from UITableView works perfectly and table view also fixed at same position.

tableView.sectionHeaderHeight = 10;
tableView.estimatedSectionHeaderHeight = UITableViewAutomaticDimension;

remove above 2 lines if added.

tableView.estimatedRowHeight = 15;
tableView.rowHeight = UITableViewAutomaticDimension;*

Row with Automatic Dimension will not create any issues.

But section header view with Automatic Dimension will create issue.

Thanks.

0
votes

This is a know bug with the UITableViews, it's been there quite a long time and hasn't been fix (by the looks of it, it never will).

The root of this is because your cells are of dynamic sizes and (I'm assuming) you are using the default 'UITableViewAutomaticDimension'. To fix this you need to drop the use of 'UITableViewAutomaticDimension' and calculate the size of each cell. This should help with the bounciness of the table view but probably wont be perfect.

Another way is to rely on CATransaction to lock and keep the table view position after the insert.

It should look something like this (probably, I didn't test it):

CGFloat offset = self.tableView.contentSize.height - self.tableView.contentOffset.y;

[CATransaction begin];
[CATransaction setDisableActions:YES];

[CATransaction setCompletionBlock: ^{
    // Code to be executed upon completion
}];

[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths: indexPaths
                 withRowAnimation: UITableViewRowAnimationAutomatic];

self.tableView.contentOffset = CGPointMake(0,
                                        self.tableView.contentSize.height - bottomOffset);
[self.tableView endUpdates];
[CATransaction commit];