我有一种方法可以从外部URL检索数据,将其从JSON格式加载到数组中,然后填充UITableView。它工作正常,但是没有向用户指示在下载数据时发生了某些事情。

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self retrieveDataC];

}


这是我为viewDidLoad尝试的代码,该代码在下载时添加了微调器动画。我试图将retrieveDataC放在后台线程上,当它完成时,我希望视图继续执行,好像我没有在上面的示例中实现多线程一样。

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    spinner.center = CGPointMake(160, 240);
    [self.view addSubview:spinner];
    [spinner startAnimating];

    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [self retrieveDataC];
        dispatch_async(dispatch_get_main_queue(), ^{
        [spinner stopAnimating];
        });
    });

}


加载微调器会在短时间内正确显示,但是在完成该过程之后,我留下空白表,好像我没有调用[self retrieveDataC]一样。有什么建议,建议吗?我是否正确设置了后台进程?

谢谢

编辑:

这就是最终的工作-

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    spinner.center = CGPointMake(160, 240);
    [self.view addSubview:spinner];
    [spinner startAnimating];

    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [self retrieveDataC];
        dispatch_async(dispatch_get_main_queue(), ^{
        [spinner stopAnimating];
        [self.collectionView reloadData];
        });
    });

}

最佳答案

收到数据后,您是否拨打[self.tableView reloadData]

关于ios - 从外部URL加载方法后,继续加载 View ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23325805/

10-14 22:20
查看更多