Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone UITableView PlainStyle with custom background image - done "entirely" in code

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!

like image 271
RickiG Avatar asked Oct 28 '09 14:10

RickiG


3 Answers

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
like image 172
coneybeare Avatar answered Oct 20 '22 20:10

coneybeare


All of this jumping through hoops is unnecessary if you're targeting > iOS 3.2, where you can use the new backgroundView property to set a UIImageView directly, e.g.:

// In your UITableViewController subclass
- (void)viewDidLoad
{
    [super viewDidLoad];

    UIImageView *view = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.png"]];
    self.tableView.backgroundView = view;
}  
like image 27
voyce Avatar answered Oct 20 '22 18:10

voyce


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;

}

like image 2
RickiG Avatar answered Oct 20 '22 18:10

RickiG