表为空时,iPhone UITableView图像背景

bas*_*han 6 iphone background objective-c uitableview

当我的UITableView为空时,我想显示图像背景.目前我尝试将UIImageView添加到包含表的视图控制器,但XCode不允许它.

这样做有好办法吗?

sim*_*nbs 15

您可以在表视图的顶部添加图像视图,也可以更改表视图的背景视图.

// 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;
}
Run Code Online (Sandbox Code Playgroud)