0

我有一个表格视图,用于显示从 Web 服务加载的数据。有时它加载速度很快,有时它需要一段时间。在加载时,表格视图只显示一个空白屏幕(在我的情况下它是一个灰色屏幕)。我想在 tableview 背景视图中显示一个简单的图像,上面写着正在加载,并且有一个加载图标,我将动画旋转,但我根本无法让它显示出来。这是我的代码:

UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, self.view.bounds.size.height)];
[backgroundView setBackgroundColor:[UIColor colorWithRed:227.0 / 255.0 green:227.0 / 255.0 blue:227.0 / 255.0 alpha:1.0]];

self.tableLoading.image = [UIImage imageNamed:@"loading.png"];
self.tableLoading.frame = CGRectMake(145, 80, 30, 30);
[backgroundView addSubview:self.tableLoading];

[self.feedTableView setBackgroundView:backgroundView];

背景视图按预期显示,因此是灰色背景,但我根本无法显示图像。

有什么建议么?这似乎是一个简单的问题,但我已经花了很长时间没有成功。提前致谢!

4

2 回答 2

0

您可以避免使用 UIView,而可以使用活动指示器。尝试这样的事情:

-(void)viewDidLoad{
    UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, self.view.bounds.size.height)];
    backgroundView.tag=1;
    [backgroundView setBackgroundColor:[UIColor colorWithRed:227.0 / 255.0 green:227.0 / 255.0 blue:227.0 / 255.0 alpha:1.0]];

    self.tableLoading.image = [UIImage imageNamed:@"loading.png"];
    self.tableLoading.frame = CGRectMake(145, 80, 30, 30);

    [backgroundView addSubview:self.tableLoading];
    [self.view addSubview:backgroundView];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    // Do your web service call asynchronous
        dispatch_sync(dispatch_get_main_queue(), ^{
            //All UI needs to be here, in this thread
            // remove from superview your loading image to show your tableview content
            for (UIView *subview in [self.view subviews]) {
                if (subview.tag == 1) {
                    [subview removeFromSuperview];
                }
            }
            [self.yourTableView reloadData];
        });
    });
}
于 2013-08-01T03:15:36.383 回答
0

你的网络服务调用如何?我想它是在异步块上完成的。也许像AFNetworking NSOperation块一样的东西?如果是这样,您在哪里将图像设置为背景?所有用户界面(UI)的东西都不应该在后台线程上完成,它应该在主线程上完成,因为主线程是唯一应该参与 UI 的线程。

尝试以下操作:

 dispatch_async(dispatch_get_main_queue(), ^{

    UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, self.view.bounds.size.height)];
    [backgroundView setBackgroundColor:[UIColor colorWithRed:227.0 / 255.0 green:227.0 / 255.0 blue:227.0 / 255.0 alpha:1.0]];

    self.tableLoading.image = [UIImage imageNamed:@"loading.png"];
    self.tableLoading.frame = CGRectMake(145, 80, 30, 30);
    [backgroundView addSubview:self.tableLoading];

    [self.feedTableView setBackgroundView:backgroundView];

});

您可以UITableView在启动请求时开始显示背景图像,并在请求完成联网后立即将其关闭。

例如,SLRequest有一个被调用的发送请求方法performRequestWithHandler:,它有一个在请求完成时要调用的处理程序,在该处理程序中,您可以关闭自定义指示器或将其从视图控制器中删除。

希望这有帮助。

于 2013-08-01T02:58:08.127 回答