views:

772

answers:

3

Hi all,

I'm working on trying to obtain the UIView that UITableView scrolls over, if scrolling is enabled. Typically, the background is white, and if you push the UITableView out of its bounds, you'll see a background. I'm trying to set this background to a UIColor of blackColor. I can't seem to find the appropriate one to tag. I've tried the following code in my UIViewController:

- (void)loadView 
{
 [super loadView];
 UITableView *aTableView = 
  [[[UITableView alloc] 
   initWithFrame:self.view.bounds
   style:UITableViewStylePlain] autorelease];

 [aTableView setScrollEnabled:YES];
 [self.view setBackgroundColor:[UIColor blackColor]];
 [self.view addSubview:aTableView];
 self.tableView = aTableView;
}

The color still stays white. Seems I'm hitting the wrong UIView.

Any idea? Thanks.

A: 

I don't have a solution but I do have a suggestion. Have you tried making the UITableView's backgroundColor transparent and setting the color of the superview to white?

Roger Nolan
Hey Roger, I don't want the blackColor to seep into my UITableView. I want the normal styling for it. I just want to see the background when the table gets scrolled out of its bounds.
Coocoo4Cocoa
A: 

I was able to achieve what you want, but it looks like a workaround to me (maybe there's a bug on the libraries).

First, you need to set the backgroundColor property to black in the UITableView.

Then, when you create a new UITableViewCell you need to do something like this (this code goes on the UITableView's datasource):

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *MyIdentifier = @"MyIdentifier";

    // Try to retrieve from the table view a now-unused cell with the given identifier
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

    // If no cell is available, create a new one using the given identifier
    if (cell == nil) {
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
    }
    cell.contentView.backgroundColor = [UIColor whiteColor];

    UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 60, 20)] autorelease];
    label.text = @"test";
    label.textColor = [UIColor blackColor];
    [cell.contentView addSubview:label];
    cell.textColor = [UIColor blackColor];

    return cell;
}

You have to set the background color of the contentView to white, but when you do it the cell's text stop appearing, so you need to add a UILabel to its content view and write the text yourself.

It seems overkill just to change a background color, but there's probably a bug that prevents setting a different background color on the UITableViewCell's than on its containing UITableView.

pgb
A: 

Hmm.. I run in a similar problem setting the background color of a cell in the cellForRowAtIndexPath method. The solution was to set the background color in the callback method willDisplayCell.

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
   cell.backgroundColor = (indexPath.row % 2 == 0) ? DARK_BACKGROUND : LIGHT_BACKGROUND;
}
MacTouch