tags:

views:

23

answers:

2

I'm trying to write code to modify the background color of a cell based on it's position in the table. While the following code 'works', it only effects cells that are passed to it. Empty cells don't get effected.

- (void)tableView: (UITableView*)tableView willDisplayCell: (UITableViewCell*)cell forRowAtIndexPath: (NSIndexPath*)indexPath
{
    if(!indexPath.row%2)
    {
        cell.backgroundColor = [UIColor colorWithRed: 0.0 green: 0.0 blue: 1.0 alpha: 1.0] ;
        NSLog(@"Blue");
    }
    else{
        cell.backgroundColor = [UIColor whiteColor];
        NSLog(@"white");
    }
    cell.textLabel.backgroundColor = [UIColor clearColor];
    cell.detailTextLabel.backgroundColor = [UIColor clearColor];
}

How can I effect the rest of the cells that are displayed, if empty, when there are few items in the list?

A: 

You should implement

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

in your delegate and initialize cells as you want.

Artem Tikhomirov
Unfortunately, that doesn't seem to work. Adding the code: cell.backgroundColor=[UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0]; doesn't do anything to change any of the cells, they all come out the default white-grey color.
RonLugge
+1  A: 

I guess you want to change the background only of "empty" cells ... Then you can change the backgroundColor in the InterfaceBuilder of the UITableView. But it will affect all empty cells.

You can also change the separator type.

Also, have a look here : http://www.zenbrains.com/blog/en/2010/06/color-de-fondo-de-celdas-vacias/

EDIT :

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // 7 depends on how many cell is shown
    return ([source count] <= 7) ? 7 : [source count];
}

From : http://stackoverflow.com/questions/2614242/show-blank-uitableviewcells-in-custom-uitableview

William Remacle
I want to effect all cells -- I want all of the cells displayed to bounce back and forth between the two indicated colors. Alas, the link you displayed doesn't provide useful source; if I try and set the background color of the contentView, it doesn't change the entire cell's color properly. There is a 'strip' of unchanged color on the right. And changing it so I'm changing selectedBackgroundView, or just backgroundView, doesn't do anything either.
RonLugge
I edit my answer : you need to add by yourself extra cells, and now in cellForRowAtIndexPath all cells will be customizable.
William Remacle
The problem is I don't have those extra cells (in terms of the source data), which causes other unintended results.
RonLugge