iPhone UITableView的图像背景时,表是空的 [英] iPhone UITableView image background when table is empty

查看:194
本文介绍了iPhone UITableView的图像背景时,表是空的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示的图像背景,当我的UITableView是空的。
目前,我尝试添加一个UIImageView到包含一个表我的视图控制器,但是X code不答应。

I would like to show an image background when my UITableView is empty. Currently I tried adding a UIImageView to my view controller that contains a table, but XCode doesn't allow it.

有没有做这件事的好方法吗?

Is there a good way of doing it?

推荐答案

您既可以在表格视图顶部添加图像查看或更改表视图的背景图。

You could either add an image view on top of the table view or change the background view of the table view.

// Check if table view has any cells
int sections = [self.tableView numberOfSections];
BOOL hasRows = NO;
for (int i = 0; i < sections; i++) {
    BOOL sectionHasRows = ([self.tableView numberOfRowsInSection:i] > 0) ? YES : NO;
    if (sectionHasRows) {
        hasRows = YES;
        break;
    }
}

if (sections == 0 || hasRows == NO)
{
    UIImage *image = [UIImage imageNamed:@"test.png"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

    // Add image view on top of table view
    [self.tableView addSubview:imageView];

    // Set the background view of the table view
    self.tableView.backgroundView = imageView;
}

这篇关于iPhone UITableView的图像背景时,表是空的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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