JAHelia
JAHelia

Reputation: 7922

How to change the backgroundColor for all UITableViewCells

I have a very simple view controller which has only a UITableView and a UIButton, when tapping the button, I want to change the color of the background of all UITableViewCells to green, giving that there are some cells not visible, I use this loop to accomplish what I need:

 - (IBAction)click:(id)sender {

for (int row = 0; row < [self.tableView numberOfRowsInSection:0]; row++)  {

    NSIndexPath* cellPath = [NSIndexPath indexPathForRow:row inSection:0];
    UITableViewCell* cell = [self.tableView cellForRowAtIndexPath:cellPath];

    cell.backgroundColor = [UIColor greenColor];

 }

}

the problem is with the default behavior of the UITableView, it does not actually create the invisible cells until they are visible !! so the above code unfortunately works ONLY on visible cells. The question is, how can I change the color of all cells on button tap ?

p.s. this very simple project sample can be downloaded here.

thank you in advance.

Upvotes: 3

Views: 1367

Answers (6)

Jignesh Chanchiya
Jignesh Chanchiya

Reputation: 803

In your code just change this method

may be this is your temporary solution

-(IBAction)click:(id)sender 
{

    self.tableView.backgroundColor=[UIColor greenColor];

}

Upvotes: 1

Ankit Gupta
Ankit Gupta

Reputation: 958

this link can solve your problem ,it have similar problem like your Setting background color of a table view cell on iPhone

Upvotes: 0

acorc
acorc

Reputation: 360

You want to use the UITableViewDataSource to handle this, so just create a UIColor variable in your .h file, change the variable in your action and tell the tableView to reload it's data, and then set the color in the data source response.

MyTableViewController.h

@interface MyTableViewController : UITableViewController {
    UIColor *cellColor;
}
-(IBAction)click:(id)sender;

MyTableViewController.m

@implementation MyTableViewController

    -(IBAction)click:(id)sender{
        cellColor = [UIColor redColor];
        [self.tableView reloadData];
    }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        // Setup your cell
        cell.textLabel.backgroundColor = [UIColor clearColor];
        cell.detailTextLabel.backgroundColor = [UIColor clearColor];
        cell.accessoryView.backgroundColor = [UIColor clearColor];
        cell.contentView.backgroundColor = cellColor;
        return cell;
    }

@end

Upvotes: 2

adali
adali

Reputation: 5977

you don't have to do like this

just use a variable to save the backgroundColor of cells

UIColor cellColor;

then when you change the color call

[tableView reloadData];

then in

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     UITableViewCell *cell = yourcell;

     ...
     ...
     ...

     cell.backgroundColor = cellColor;

     return cell ;
}

it's done!

update

sorry , try this

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     UITableViewCell *cell = yourcell;

     ...
     ...
     ...
    cell.textLabel.backgroundColor = [UIColor clearColor];
UIView* bgview = [[UIView alloc] initWithFrame:cell.bounds];
bgview.backgroundColor = [UIColor greenColor];
    [cell setBackgroundView:bgview];

     return cell ;
}

Upvotes: 4

Paul Hunter
Paul Hunter

Reputation: 4463

Store the color as a retained property and set it when you click the button.

@property (retain, nonatomic) UIColor *cellColor;

....

- (IBAction)click:(id)sender {
    self.cellColor = [UIColor greenColor];

    for (int row = 0; row < [self.tableView numberOfRowsInSection:0]; row++)  {

        NSIndexPath* cellPath = [NSIndexPath indexPathForRow:row inSection:0];
        UITableViewCell* cell = [self.tableView cellForRowAtIndexPath:cellPath];

        cell.contentView.backgroundColor = self.cellColor;
        cell.textLabel.backgroundColor = self.cellColor;
    }

}

You could also choose to reload the whole tableView instead of iterating through the cells.

- (IBAction)click:(id)sender {
    self.cellColor = [UIColor greenColor];    
    [self.tableView reloadData];
}

Then in your tableView:cellForRowAtIndexPath: check if the property is set and set the contentView and textLabel background colors.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ...
    if (self.cellColor) {
        cell.contentView.backgroundColor = self.cellColor;
        cell.textLabel.backgroundColor = self.cellColor;
    }
    return cell;
}

Upvotes: 1

fguchelaar
fguchelaar

Reputation: 4909

You state in a comment to Paul Hunter's answer "does not color the whole cells".

Try setting a backgroundView:

cell.backgroundView = [[[UIView alloc] init] autorelease]; 
cell.backgroundView.backgroundColor = self.cellColor;

Upvotes: 1

Related Questions