John Stewart
John Stewart

Reputation: 1176

How do you access tableView in Master-Detail storyboard template?

I'm using storyboarding and the Master-Detail Core Data template in Xcode4.

I want to set the background image for my tableView on the master view controller.

To do this was simple for the iPhone, I just did this in viewDidLoad of the MasterViewController.m:

self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]];

However, this doesn't work for the iPad; the default background is being shown instead in the master tableView.

I wouldn't think this was a splitViewController issue; from the perspective of the MasterViewController object, he should always have the same tableView, right? In fact, I know this is the case, because this code works on both iPad and iPhone, a line above the "backgroundColor" assignment, and this is using the same "self.tableView" object:

self.tableView.separatorColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"separator.png"]];

This is what it looks like on the iPhone, with both the separator and background image working (sorry, can't post pictures inline yet):

https://i.sstatic.net/JaULS.png

And, using the same code, on the iPad:

https://i.sstatic.net/vVKmt.png

What do I have to set in order to set the background image on the master tableView?

Upvotes: 1

Views: 797

Answers (2)

John Stewart
John Stewart

Reputation: 1176

Aha! I found the secret sauce:

UIImage *backgroundImage = [UIImage imageNamed:@"foo.png"];
UIView *backgroundView = [[UIView alloc] init];
[backgroundView setBackgroundColor:[UIColor colorWithPatternImage:backgroundImage]];
[self.tableView setBackgroundView:backgroundView];

The key is using [self.tableView setBackgroundView:...]instead of the property self.tableView.backgroundColor = ...

Upvotes: 1

James
James

Reputation: 2376

You need to make the background clear in interface builder. Then, you need to say self.tableView.backgroundColor = [UIColor clearColor]; in your viewDidLoad. Finally, put a UIView behind it with the appropriate background color. For some reason, the background stuff is overridden in the UITableView itself.

Upvotes: 2

Related Questions