How to add a background image to a table view

- by

The easiest way to do this is by adding a UIImage to your table. Rather than instantiating a new table object you can simply add the following line into the first Table View Data Source method you have – such as this one:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    tableView.backgroundView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"background.jpg"]];
    
    // Return the number of sections.
    return 1;
}

Just make sure you do this before the return statement (which ends the method, ignoring all code beyond).

Note that in grouped tables the cell object has its own background. You can set its colour in Interface Builder (use clearColour if you’d like your background to shine through) or you can use the tableView:cellForRowAtIndexPath method to override it separately with something else:

cell.backgroundView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"another-background.jpg"]];

Again make sure you add this statement before the return cell call.



If you enjoy my content, please consider supporting me on Ko-fi. In return you can browse this whole site without any pesky ads! More details here.

Leave a Comment!

This site uses Akismet to reduce spam. Learn how your comment data is processed.