Search code examples
iosuitableviewasihttprequestdelay

How To Ensure User Sees Row Insert Animation iOS


I've got an app that displays information from our web service. The user has the option to add rows to a table view to display more information. I'm using:

[self.tableView insertSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];

to insert the row. It gives a nice bit of animation so the user knows where the row is they just inserted. My problem is that the row being added has to be in view in order for the animation to be seen. So I scroll down so that the row to be added is in view:

[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];

then start the ASIHTTPRequest asynchronously to get the info for the new row.

If there is a decent amount of data, the animation shows great. Problem is, if the user is on WiFi or not pulling much info, the ASIHTTPRequest returns before scroll completes and the new row is just there (the user doesn't see it get added). If I force a delay (.03 seconds) between the call to scroll the Table View and start the request, I get the desired effect, but then there is a longer delay than necessary on the loads that were already being displayed as desired.

I thought I would try to save the time, in ms, from when the view was scrolled and the request finished, and make sure it was longer that .03 seconds, but I kept getting an error with

[NSDate timeIntervalSinceReferenceDate]

so I figured I'd ask here if there was a better way before I hacked into that solution more.

Thanks!


Solution

  • Using a combination of timers and a couple flags will help you out, which is basically what you are already doing.