views:

4075

answers:

4

Hi

I have been all over the place, seems the UITableView with a static background issue is well documented, but no one with a straight forward solution? Im building my TableViews entirely in code, like this:

    UIViewController *tableViewController = [[TableViewController alloc] init];
navigationController = [[UINavigationController alloc]
      initWithRootViewController:tableViewController];
[tableViewController release];
[window addSubview:navigationController.view];

The window is my main UIWindow build for me in the app delegate. From here on I need to build a few different TableViews (controlled by the navigationController), some with fetchedResultsControllers, custom cells and so on. I prefer to do this completely in code, not using nib's as this would result in either having customization spread between code and IB or having to build and maintain 6+ different Nibs.

I simply can't find a working example where a tableViewController Class sets it's own background image. If I do this inside one of my TableViews (extending UITableViewController):

self.tableView.backgroundColor = backgroundColor;

I, of course, get the tableView's background colored (which incidentally colors the cell's as well, think the cell's inherits their color from the tableView?) but I wish to have a static background image that my cells slide up and down on top of. Not a "background image" that slides up and down with the users gestures. Exactly what the GroupedStyle tableView offers, but in a PlainStyle tableView:) .. and done using code, not IB.

I guess I have to clear the background color of the table view, then set the Cells color when configuring them so they don't turn out transparent. And then somehow "sneak" a background image below the tableView view from inside the tableView instance?

How will I go about this, the best solution would to be able to do this in viewDidLoad or any other function inside my TableViewController, to keep all my customization in one place.

Hope someone can help me, Im all 'googled out' :) Thanks!

+5  A: 

You need to set up your controller as a UIViewController, not a UITableViewController. Then add the tableview programmatically above a background imageView.

@interface SomeController : UIViewController <UITableViewDataSource, UITableViewDelegate> {
  ...
  UITableView *tableView;
  ...
}
@property (nonatomic, retain) UITableView *tableView;
@end



@implementation SomeController

@synthesize tableView;

...

- (void)loadView {
    [super loadView];
    UIImageView *v = [[[UIImageView alloc] initWithFrame:self.view.bounds] autorelease];
    [v setImage:[UIImage imageNamed:@"table_background.png"]];
    [self.view addSubview:v];


    self.tableView = [[[UITableView alloc] initWithFrame:self.view.bounds] autorelease];
    [self.tableView setBackgroundColor:[UIColor clearColor]];
    [self.view addSubview:self.tableView];
}
...
@end
coneybeare
Thanks Coneybeare, this seems to follow the logic that I have seen in Nibs that incorporate a custom image, build generic view, put an imageview and a tableView inside it. I do get some strange behavior :/ I implemented the cellForRowAtIndexPath: and it has a local variable called "tableView" as well. This causes the compiler warning "local declaration of 'tableView' hides instance variable". I then tried renaming the variable to tableViewLocal but the warning persists. -continue...
RickiG
Also when loading up the view (as described in my question) it crashes when I add the [self.tableView addSubview:self.tableView];Isn't it supposed to be :[self.view addSubview:self.tableView]; this compiles and works, but there is only the background image, no tables?I think the approach is sound, but there is something Im missing:)Thans again:)
RickiG
I would change all the "tableView" arguments in the method signatures to be theTableView, and leave self.tableView as it is. The crash was a typo on my part, it should have been self.view add… (fixed now)
coneybeare
self.tableView = [UIView alloc] should be self.tableView = [UITableView alloc]. For me that was enough to fix the crash.
paul_sns
A: 

Thanks Coneybeare.

It doesn't crash anymore and the background image turns up just perfect (along with my navigationController in the top) However, still no visible tableView? just the background image and the navigationControllerBar:

This is my implementation:

- (void)loadView {
[super loadView];

UIImageView *imageView = [[[UIImageView alloc] initWithFrame:self.view.bounds] autorelease];
[imageView setImage:[UIImage imageNamed:@"carbon_background.png"]];
[self.view addSubview:imageView];

[self.tableView = [[UIView alloc] initWithFrame:self.view.bounds] autorelease];

[self.tableView setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:self.tableView];

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)theTableView {
return 1;

}

- (NSInteger)tableView:(UITableView *)theTableView numberOfRowsInSection:(NSInteger)section {
return 3;

}

- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [theTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = @"Hello, World";

return cell;

}

//commit edit, didSelectRow, memory … etc.

The forum wasn't up for an entire .m file in one go. Please tell me if I left something out that could help indicate an error.

I thought maybe it was the order of the layers and tried this without any luck:

    [self.view sendSubviewToBack:imageView];

Hope I missed something obvious.

Thanks for your time:)

RickiG
Just got it:)self.tableView = [[[UIView alloc] initWithFrame:self.view.bounds] autorelease];needs to be:self.tableView = [[[UITableView alloc] initWithFrame:self.view.bounds] autorelease];Else it won't "fit" :)Thanks again!
RickiG
+1  A: 

Ok, now it is running:) My tableView was not populated with my cells, so breakPointing through the thing I found out that even though I had implemented the TableViewDataSource and TableViewDelegate, this was only in the main view, I needed to set the delegate and datasource of the tableview to = self. For others seeking an answer to this here is the method as it ended up with Coneybeares help:

- (void)loadView {
[super loadView];

UIImageView *imageView = [[[UIImageView alloc] initWithFrame:self.view.bounds] autorelease];
[imageView setImage:[UIImage imageNamed:@"carbon_background.png"]];
[self.view addSubview:imageView];

[self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds] autorelease];
[self.tableView setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:self.tableView];

self.tableView.delegate = self;
self.tableView.dataSource = self;

}

RickiG
A: 

Very Nice You just fix my day man. I been dealing with this table view background for my table view. Thanks a lot

edgard