iOS将图像添加到Tableview背景视图

iOS将图像添加到Tableview背景视图,ios,uitableview,uiview,Ios,Uitableview,Uiview,我有一个tableview,用于显示从web服务加载的数据。有时加载速度很快,有时需要一段时间。加载时,tableview只显示一个空白屏幕(在我的例子中,它是一个灰色屏幕)。我想在tableview背景视图中显示一个简单的图像,该图像表示正在加载,并且有一个加载图标,我将对其设置动画以旋转,但我根本无法让它显示出来。这是我的密码: UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, self.

我有一个tableview,用于显示从web服务加载的数据。有时加载速度很快,有时需要一段时间。加载时,tableview只显示一个空白屏幕(在我的例子中,它是一个灰色屏幕)。我想在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];
背景视图按预期显示,因此是灰色背景,但我根本无法显示图像


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

您的Web服务呼叫进展如何?我想这是在异步块上完成的。可能类似于
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:
的发送请求方法,该方法有一个在请求完成时调用的处理程序,在该处理程序中,您可以关闭自定义指示符或将其从视图控制器中删除


希望有此帮助。

您可以避免使用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];
        });
    });
}

我不认为当内容在前面时,显示后台加载对于tableview来说是一种良好的用户体验。该图像必须多久加载一次。也许在图像准备好之前隐藏表视图?其想法是仅在没有表视图数据时显示加载。最好是在加载数据之前隐藏tableview,然后将“我的图像”添加到常规视图并设置动画。我特别想要一个简单的自定义活动指示器,它只在等待数据时旋转图像,然后在加载数据时消失。这只会在从我的菜单加载视图控制器时发生。实际上,这是一个使用SLRequest的twitter调用。基本上我只是想在屏幕中间做一个自定义活动指示器,当没有TabelVIEW数据,然后当数据加载时,TabLVIEW应该自动覆盖图像,然后我就可以去掉它。我更新了我的帖子,请看一看。我以前没有使用过
SLRequest
,但我确信这与网络的想法是一样的。这是我最初的想法,但我希望有一个使用简单图像的自定义活动指示器。工作起来很有魅力!谢谢!