How to add a background image to a table view

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.





Jay is the CEO and founder of WP Hosting, a boutique style managed WordPress hosting and support service. He has been working with Plesk since version 9 and is a qualified Parallels Automation Professional. In his spare time he likes to develop iOS apps and WordPress plugins, or draw on tablet devices. He blogs about his coding journey at http://wpguru.co.uk and http://pinkstone.co.uk.