How to change backgroundColor for all UITableViewCells

I have a very simple view controller that only has UITableViewand UIButton, when I click on the button, I want to change the background color of all UITableViewCellsto green, indicating that there are some I don’t see, 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 UITableViewdefault behavior , it actually does not create invisible cells until they are visible !! therefore, the above code, unfortunately, works ONLY on visible cells. The question is, how do I change the color of all cells when I click a button?

ps this simple example project can be downloaded here .

thank you in advance.

+3
source
6

backgroundColor

UIColor cellColor;

[tableView reloadData];

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

     ...
     ...
     ...

     cell.backgroundColor = cellColor;

     return cell ;
}

!

,

- (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 ;
}
+4

UITableViewDataSource , UIColor .h , tableView , .

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
+2

property , .

@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;
    }

}

tableView .

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

tableView:cellForRowAtIndexPath: , property contentView textLabel.

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

You state in a commentary on Paul Hunter that the answer is "not staining whole cells."

Try setting backgroundView:

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

In your code just change this method

maybe this is your temporary solution

-(IBAction)click:(id)sender 
{

    self.tableView.backgroundColor=[UIColor greenColor];

}
+1
source

this link may solve your problem, it has a similar problem, for example, your Setting the background color of the table cell on the iPhone

0
source

All Articles