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.
I want add additional condition. If your code in iOS11 or more, you need do like below;
tableView.estimatedRowHeight = 0 tableView.estimatedSectionHeaderHeight = 0 tableView.estimatedSectionFooterHeight = 0
Simple solution to disable animations
@Dean's way of using an image cache is too hacky and I think it destroys the responsiveness of the UI.
One proper way: Use a UITableView subclass and override -setContentSize: in which you can by some means calculate how much the table view is pushed down and offset that by setting contentOffset.
This is a simplest sample code to handle the simplest situation where all insertions happen at the top of table view:
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.
Everyone loves copy and pasting code examples, so here's an implementation of Andrey Z.'s answer.
This is in my
delegateDidFinishUpdating:(MyDataSourceDelegate*)delegate
methodThe
NSLog
at the bottom can be replaced with a call to show a view that indicated there's fresh data.had the same problem and found a solution.
like this:
works perfectly, without glitches.