I am trying to load a table view from a cache very quickly and have the cached data in开发者_开发百科 the table view appear. Then I want download new data, and then reload the table. Right now I am downloading the new data on viewDidAppear, but the view still refreshes before it displays. Any idea how I can do this?
viewDidAppear
is not a good place to download data; it is really intended for clean up after presenting data, so I can understand why you used it. You should request your data reload as early as possible, such as in viewDidLoad
or viewWillAppear
(depending on your reuse or otherwise of view controllers).
If you are doing asynchronous downloads, which you should be, put the reloadData
call in your delegate callback function for when the data is completed.
Simply calling [tableView reloadData]
after the download might do the trick. This will trigger a refresh of your table cells.
To download the new data, you may consider using Cocoa Streams, particular an asynchronous Socket Stream. In the stream delegate, call reloadData
when the download is completed.
I ended up implementing the delegate class to do this asynchronously. This example was extremely helpful, and I implemented much of its code:
http://developer.apple.com/iphone/library/samplecode/URLCache/Introduction/Intro.html#//apple_ref/doc/uid/DTS40008061-Intro-DontLinkElementID_2
精彩评论