UITableView 加载数据并显示 HUD 时出现问题
我有一个带有导航控制器的UITableView。表视图由自定义单元格组成,每个单元格包含从服务器下载的数据,显然这种下载非常耗时,因此我想在发生这种情况时显示某种 HUD。
我的问题是这样的,当我按下按钮转换到表视图时,我按下的按钮会卡在“突出显示”状态,直到下载所有数据,然后视图从前一个视图转换为显示表视图在其所有的荣耀中。但是,我更愿意转移到空表视图,然后在填充表时显示正在加载的 HUD。 (或者类似的东西,任何东西都可以向用户显示程序实际上正在做某事并且没有崩溃。)
这是我的一些代码:
- (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 ([stories count] == 0) {
NSString * path = @"http://myURL.com";
[self parseXMLFileAtURL:path];
}
}
我理解为什么视图不会在 viewDidAppear< 之前转换并显示 HUD /strong> 被调用,只是不知道如何解决这个问题?大部分工作是由viewDidAppear开始的。
任何帮助将不胜感激,
杰克
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 ([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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你应该使整个事情异步。我的意思是,一旦你到达你的表,你将其呈现为空,为其分配一个没有数据的数据源,使 HUD 出现,然后启动一个线程从远程服务器加载数据。
线程完成其任务后,您可以通过您之前已将表绑定到的应用程序发送通知。因此,当表收到线程已完成其任务的通知时,您可以更新数据源,关闭 HUD,然后重新加载表。
如果您需要更多详细信息,请询问。
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.