I have a UITableView that I am loading via JSON. So my logic goes like this:
- Fetch the JSON (NSOperation, calling back to main thread)
- In callback, parse the json, and insert new data into my table’s data source array.
- Call reloadData on the TableView to show the new data.
My question is: How can I ANIMATE the arrival of new rows in the table? Right now I just have them all popping into existence at once, and what I want to do is make them animate into place, because that looks way cooler.
I think what I want is to use:
[self.theTableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationRight];
But that gives me runtime complaints about the size of the update being different than the size in the data source.
Am I missing something about how that method should be used? Does anyone know of an example where somebody is animating new rows into a TableView via this pattern?
Update: Basically, I solved this thanks to Ben’s guidance, with the following approach:
- Start with [self.theTableView beginUpdates];
- When I loop over my entries in the JSON, insert them at the beginning of my array, incrementing their index each time through the loop.
-
Call insertRowsAtIndexPath each time through the loop, like this:
NSIndexPath *indexPath;
indexPath = [NSIndexPath indexPathForRow:[self.theChatEntries indexOfObject:message] inSection:0];
[self.theTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:NO]; -
End with endUpdates outside of the loop.
Woot.
You’re on the right track; you need to wrap your calls to -insertRowsAtIndexPaths:withRowAnimation: with a [self.theTableView beginUpdates] and [self.theTableView endUpdates].