Keep uitableview static when inserting rows at the top

Dean Smith picture Dean Smith · Nov 25, 2010 · Viewed 29.2k times · Source

I have a tableView that I'm inserting rows into at the top.

Whilst I'm doing this I want the current view to stay completely still, so the rows only appear if you scroll back up.

I've tried saving the current position of the underlying UIScrollview and resetting the position after the rows have been inserted but this results in a judder, up and down, although it does end up back in the same place.

Is there a good way of achieving this ?

Update: I am using beginUpdate, then insertRowsAtIndexPath, endUpdates. There is no reloadData call.

scrollToRowAtIndexPath jumps to the top of the current cell (saved before adding rows).

The other approach I tried, which ends up in exactly the right pace, but with a judder is.

save tableView currentOffset. (Underlying scrollView method)
Add rows (beginUpdates,insert...,endUpdates) 
reloadData ( to force a recalulation of the scrollview size )
Recalculate the correct new offset from the bottom of the scrollview
setContentOffset (Underlying scrollview method)

Trouble is the reloadData causes the scrollview/tableview to start scrolling briefly, then the setContentOffset returns it to the correct place.

Is there a way of getting a tableView to work out it's new size without starting display ?

Wrapping the whole thing in a beginAnimation commitAnimation doesn't help much either.

Update 2: This can clearly be done - see the offical twitter app for one when you pull down for updates.

Answer

AmitP picture AmitP · Mar 10, 2014

There's really no need to sum up all rows height, the new contentSize after reloading the table is already representing that. So all you have to do is calculate the delta of contentSize height and add it to the current offset.

    ...
    CGSize beforeContentSize = self.tableView.contentSize;
    [self.tableView reloadData];
    CGSize afterContentSize = self.tableView.contentSize;

    CGPoint afterContentOffset = self.tableView.contentOffset;
    CGPoint newContentOffset = CGPointMake(afterContentOffset.x, afterContentOffset.y + afterContentSize.height - beforeContentSize.height);
    self.tableView.contentOffset = newContentOffset;
    ...