Question

I have a view set up (more or less) like this:

-- ViewController

--- ScrollView

--- ContentView

---- ContainerView

-----> TableViewController with static cells embedded inside the ContainerView

Each view has a matching outlet.

I have configured my background to a nice image that I want to show through all the controls on the page, including the embedded TableViewController.

I've referenced the table like this:

UIView *theTableView = self.contentView.subviews[0];

and tried to set the colour of the table to clear like this (to make it see-through):

theTableView.backgroundColor = [UIColor clearColor];

But this doesn't work because I think it's the cells' colour I need to change.

How can I reference the embedded table's cells in order to change the colour of the cells' background to clearColor programmatically in my situation?

Thanks!

Was it helpful?

Solution

You should set the table view's delegate and respond to tableView:willDisplayCell:forRowAtIndexPath:.

- (void)viewDidLoad {
    [super viewDidLoad];

    self.tableView = self.contentView.subviews[0]; // use a property UITableView *tableView
    self.tableView.delegate = self;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView != self.tableView) return;
    cell.backgroundColor = [UIColor clearColor];
}

This will work for both static and dynamic table views.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top