Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone UITableView image background when table is empty

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?

like image 758
bashan Avatar asked Jan 18 '12 21:01

bashan


2 Answers

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;
}
like image 128
simonbs Avatar answered Sep 19 '22 18:09

simonbs


UITableView has a backgroundView property. Set this property to the UIImageView containing your background image.

like image 39
rob mayoff Avatar answered Sep 17 '22 18:09

rob mayoff