查看上显示加载图标上方,由于​​异步调用 [英] View displaying on top of loading icon due to async call

查看:177
本文介绍了查看上显示加载图标上方,由于​​异步调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是做在单个线程一切才刚刚开始移动到异步调用,所以我有点糊涂了。

I was doing everything in a single thread and have just started moving to asynchronous calls so I'm a bit confused.

我想这样的观点来加载,无论是与一个空表或不带表都没有。该MBProgressHUD应该显示而异步调用获取数据。然后,当数据被发现HUD消失和表刷新。

What I would like is for the view to load, either with an empty table or with no table at all. The MBProgressHUD should show while the async call gets the data. Then when the data is found the HUD goes away and the table refreshes.

我现在拥有的是一切正常,除了HUD显示空桌子底下。这里是最相关的code。

What I have now is everything works except that the HUD is displayed underneath the empty table. Here is the most relevant code.

- (void)awakeFromNib
{
    [super awakeFromNib];
    [MBProgressHUD showHUDAddedTo:self.tableView animated:YES];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        self.dataController = [[StudyController alloc] initWithCredentials:email password:password];
        dispatch_sync(dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUDForView:self.tableView animated:YES];
            [self.tableView reloadData];
        });
    });
}

基本上发生了什么,我想,是异步调用运行时调用毕竟我的code被渲染表正常运行一次,并显示在平视显示器顶部的空表,然后再次当我告诉它reloadData。我如何得到它不运行第一次?

Basically what is happening, I think, is all my code that was rendering the table correctly runs once while the async call is running, and shows an empty table on top of the HUD, and then again after the call when I tell it to reloadData. How do I get it to not run that first time?

感谢。

推荐答案

这是很难没有看到你的表视图的委托/数据源的方法来衡量,但你的直觉是有道理的。在这种情况下,我可以看到两个选项:

It's hard to gauge without seeing your table view delegate/datasource methods, but your intuition makes sense. In this case, I can see two options:


  1. 添加一些逻辑到你的委托/数据源的方法来正确显示您加载视图,如果表是空的。

  2. 而不是加载视图添加到表视图中添加加载视图表视图的上海华。这样一来,就可以确保加载视图总是在表视图的顶部。

我通常选择第二种方法,因为我preFER只专注于表视图我的数据源和委托方法。

I usually opt for the second method, because I prefer to focus my datasource and delegate methods on only the table view.

希望这有助于!

这篇关于查看上显示加载图标上方,由于​​异步调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆