Vjardel
Vjardel

Reputation: 1075

iOS tableView.backgroundView with no cell

I've checked how to make background with UITableView with this code in cellForRowAtIndexPath :

UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.JPG"]];
[tempImageView setFrame:tableView.frame];
tableView.backgroundView = tempImageView;

It works fine, but when there is no cell, the background doesn't display because cellForRowAtIndexPath isn't call when there is no cell.

Alright, I would like to display background even if there is no cell, so I tried :

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.events.count == 0)
    {
        UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.JPG"]];
        [tempImageView setFrame:tableView.frame];
        //
        tableView.backgroundView = tempImageView;
    }
    return self.events.count;
}

But I don't know if it's the best choice ?

And how can I make different background size with different devices (iPhone4/5/6/6+) ? Just with image.jpg, [email protected] etc ?

Cheers!

Upvotes: 0

Views: 124

Answers (1)

user4151918
user4151918

Reputation:

This is how I approached it:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.tableView.backgroundView = [[UIImageView alloc] initWithImage:
                                     [UIImage imageNamed:@"Background0"]];
    [self.tableView.backgroundView setContentMode:UIViewContentModeScaleAspectFill];

    // Adding a table footer will hide the empty row separators

    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [cell setBackgroundColor:[UIColor clearColor]];
}

Upvotes: 1

Related Questions