UITableView Problem with loading in data and showing HUD
I have a UITableView with a Navigation Controller. The table view is made up of custom cells that each contain data that is downloaded from a server, obviously this downloading is time consuming and therefore I would like to show a HUD of some sort while this happens.
My problem is this, when I press the button to transition to the table view, the button that I press gets stuck in the "highlighted" state until all the data is downloaded, then the view transitions from the previous one to show the table view in all its glory. However, I would prefer to transfer to an empty table view, then show a loading HUD while the table is populated. (Or something similar, anything to show the user the program is actually doing something and hasn't crashed..)
Here is some of my code:
- (void) displayView:(int)intNewView{
NSLog(@"%i", intNewView);
[currentView.view removeFromSuperview];
[currentView release];
switch (intNewView) {
case 1:
currentView = [[View1 alloc] init];
break;
case 2:
currentView = [[View2 alloc] init];
break;
case 3:
currentView = [[View3 alloc] init];
break;
case 4:
currentView = [[View4 alloc] init];
break;
case 5:
vc = [[TableViewController alloc] init];
currentView = [[UINavigationController alloc] initWithRootViewController:vc];
[currentView setTitle:@"Events"];
break;
}
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
[self.view addSubview:currentView.view];
[UIView commitAnimations];
}
- (void)viewDidLoad {
[super viewDidLoad];
//Initialisation code..
[SVProgressHUD showInView:self.view status:@"Doing Stuff"];
[self viewDidAppear:YES];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSLog(@"ViewDidAppear");
if ([开发者_JS百科stories count] == 0) {
NSString * path = @"http://myURL.com";
[self parseXMLFileAtURL:path];
}
}
I understand why the view doesn't transition and show the HUD before viewDidAppear is called, just not how to get around this? The bulk of the work is started by viewDidAppear.
Any help would be greatly appreciated,
Jack
I think you should make the whole thing asynchronous. I mean, once you get to your table, you render it as empty, assigning to it a data source with no data, make a HUD appear, then you launch a thread that will load data from the remote server.
Once the thread has completed its task, you can send a notification through your app, which you have previously bound your table to. So, when the table gets the notification that the thread has completed its tasks, you update the data source, dismiss the HUD, and reload the table.
If you need more details on this, please ask.
精彩评论