Search code examples
iphonecocoa-touchuitableview

UITableView background color does not change


In a model UIViewController I have the following implementation of loadView (everything is created programmatically):

- (void)loadView {

    // Add Basic View
    UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 540, 620)];
    myView.backgroundColor = [UIColor clearColor];
    self.view = myView;
    [myView release];

    // Add NavigationBar

    // Add a BG image

    // Add Table
    UITableView *tbView = [[UITableView alloc] initWithFrame:CGRectMake(30, 80, 480, 250) style:UITableViewStyleGrouped];
    tbView.dataSource = self;
    tbView.delegate = self;
    tbView.scrollEnabled = NO;
    tbView.backgroundColor = [UIColor clearColor];
    [tbView reloadData];

    [self.view addSubview:tbView];
    [tbView release];

    // some more code
}

As you can see I set backgroundColor to clearColor, yet when I compile and run the code I always see a gray background behind the table: enter image description here

I don't understand what I am doing wrong (sounds stupid, I know), I used to have the very same code and it worked perfectly fine. I am compiling with iOS SDK 4.2.1


Solution

  • You also need to set your UITableView's backgroundView property to nil on recent (since 3.2) versions of iOS.

    As such, adding...

    tbView.backgroundView = nil;
    

    ...should sort your problems.

    That said, if you want to maintain compatibilty with pre-3.2 devices, you should check for the existence of this via the instancesRespondToSelector method before calling it.