views:

292

answers:

2

Hey everyone, this seems like it should be a simple one; I really hope it is. As always, thanks in advance!

All I'm trying to do is add a background image to a grouped style tableview. I'm using the following method in viewDidLoad:

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

Unfortunately, background.png seems to be drawn not only behind the cells, but also on the backgrounds of the cells and the headers as follows:

alt text

I know there are a few questions on this site addressing similar issues, but I'm afraid I couldn't get anything to work. http://stackoverflow.com/questions/894875/how-can-i-set-the-background-of-uitableview-the-tableview-style-is-grouped-to led me to add

self.tableView.opaque = NO;
self.tableView.backgroundView = nil;

to viewDidLoad and to add

cell.backgroundView = nil;
cell.opaque = NO;

in configuring my cells. Needless to say, it didn't work. Adding

cell.backgroundColor = [UIColor clearColor];

didn't work either. And sadly, that was my last idea. I'd really like to be able to do this without adding the background in interface builder, if possible. I rather like doing as much as I can programmatically. I hope asking that isn't too greedy. Thanks again!

* * * EDIT: * * *

I came across another approach to accomplishing this, but ran into another problem. I added the following code to viewDidLoad:

    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    bgView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]];
    [self.tableView.window addSubview:bgView];

    [self.tableView addSubview:bgView];
    [self.tableView sendSubviewToBack:bgView];

It looked promising, and it likely is, but unfortunately in gave me the following: alt text

It looks as though it either wasn't sent to the back and the headers somehow show through or the cells went to the back with it. I'm not at all sure. Any advice here would be appreciated as well.

Here's where I found the code for the edit: http://stackoverflow.com/questions/2079002/add-uiview-behind-uitableview-in-uitableviewcontroller-code

A: 

I still haven't figured out why the above doesn't work or how to modify it so that it does, but by adding the subView to the window in the App Delegate instead of the tableView here I was able to get it to work.

Instead of just getting it to function and moving on I'd like to actually learn, so I'm still curious as to where I went wrong above. As such, I'll leave this as unanswered for a time and if you can tell me my error(s) I'll give you the up vote and the green check mark. Thanks!

Arthur Skirvin
+1  A: 

I went through a similar path to what you describe here. I finally found an answer that worked for me on the following post:

http://stackoverflow.com/questions/1602438/how-to-set-the-background-of-a-uitableview-to-an-image

It was two lines of code posted by user rkb. Here's the code:

self.tableView.backgroundColor = [UIColor clearColor];

self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithData:imageData]];

dana_a