UITableView remote data issues
I am building a news app (TabBar app) where I have to load top news coming from an rss feed into a UITableView. I have managed to do that but the problem I am now facing is two folds: - At launch, the table view remains gray (no data but app is responsive) until I switch to other tabbar items and come back to see data loaded. I guess this has to do with the latency in data being gathered? But I have seen some apps like pulse gather data rather quickly. How can I do that? And how can I avoid displaying blank gray view until data is gathered. BTW, the response from webservice is fairly quick and very light weight. - Again, referring to the way pulse app works, how do I cache previous data, so that every time the app is launched, it doesnt have to fetch new data until users refreshes?
Some code samp开发者_如何学编程les would really help. I tried looking into the URLCache example but honestly thats quite confusing to me. Thank you!
It seems to me that you are not reloading your table data when the data has been retrieved. Just add [myTableVariable reloadData];
when you're done loading the rss feeds.
To cache data between different application runs you can save it to a file in your documents folder and read it at launch. Use the cached data until you have retrieved the new data. You can adapt the following code
- (BOOL)SaveData
{
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask ,YES );
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingString:@"/cache.dat"];
NSArray *userInfo = [NSArray arrayWithObjects:@"Apples", @"Bananas", @"Oranges", nil];
BOOL saved = [NSKeyedArchiver archiveRootObject:userInfo toFile:path];
return saved;
}
- (NSArray *)LoadData
{
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask ,YES );
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingString:@"/cache.dat"];
NSArray *userInfo = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
return userInfo;
}
Hope this helps
You can try showing a loading view on top of the tabBar so and remove when tableView finish displaying data.
Show loading indicator and hide tableview until you load data from RSS. You need to reload your UITableview after XML Parser finish parsing. That will work. If you don't want to load data every time for the server you can store all the data in local sqlite database.
精彩评论