3

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.

JAHelia
  • 6,934
  • 17
  • 74
  • 134
  • 1
    You don't want that to happen. UITableView renders the cells on a per-demand-basis whereas the demand is controlled by the user scrolling around. Why not changing the color within the `cellForRowAtIndexPath:` implementation? – Till Apr 18 '12 at 16:05
  • 1
    in a similar situation (in another project) there is a table view with uitextfield inside each cell, I want to check if the user has entered text or not in each cell on button tap, this cannot be accomplished with cellForRowAtIndexPath method, I need a workaround for this requirement. – JAHelia Apr 18 '12 at 16:12

6 Answers6

4

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 ;
}
adali
  • 5,977
  • 2
  • 33
  • 40
2

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
acorc
  • 360
  • 1
  • 8
  • this solution didn't work, I don't know why, but if you apply on the attached sample project it just will not work, check it and see. – JAHelia Apr 18 '12 at 16:28
  • Okay, do this instead. Replace `cell.backgroundColor = cellColor` with `cell.textLabel.backgroundColor = [UIColor clearColor]; cell.detailTextLabel.backgroundColor = [UIColor clearColor]; cell.accessoryView.backgroundColor = [UIColor clearColor]; cell.contentView.backgroundColor = cellColor;` I updated my code above to reflect this. – acorc Apr 21 '12 at 09:43
1

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;
}
Paul Hunter
  • 4,453
  • 3
  • 25
  • 31
  • (after you edited the answer) I have applied this solution on my sample project but didn't work on invisible cells. – JAHelia Apr 18 '12 at 16:22
  • Hmm.. It works in my simulator. Are you retaining the UIColor? And did you place the "if (self.cellColor)" block _outside_ of the "if (cell == nil)" block? – Paul Hunter Apr 18 '12 at 16:26
  • And are you setting contentView.backgroundColor and textLabel.backgroundColor and _not_ cell.backgroundColor? – Paul Hunter Apr 18 '12 at 16:30
  • im retaining the color and placed the if statement outside the if(cell==nil) block – JAHelia Apr 18 '12 at 16:30
  • Ive tried the three options: contentView.backgroundColor, textLabel.backgroundColor and backgroundColor all of them does not color the whole cells – JAHelia Apr 18 '12 at 16:36
  • Have you tried without _ever_ setting cell.backgroundColor = [UIColor greenColor]; ? When I set that the example no longer works. Without it it works. – Paul Hunter Apr 18 '12 at 16:42
1

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;
fguchelaar
  • 4,779
  • 23
  • 36
  • I've tried your code it colors the whole visible cells only at the edge of the cell, the invisible ones are colored correctly as desired. sounds weird, you can try it on the attached sample project – JAHelia Apr 19 '12 at 06:36
  • Ah sorry, I forgot to mention that in my solution I've set the tableview's background to ClearColor (I have a static background image behind it) – fguchelaar Apr 19 '12 at 14:17
1

In your code just change this method

may be this is your temporary solution

-(IBAction)click:(id)sender 
{

    self.tableView.backgroundColor=[UIColor greenColor];

}
Adrian P
  • 6,479
  • 4
  • 38
  • 55
0

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

Community
  • 1
  • 1
Ankit Gupta
  • 958
  • 1
  • 6
  • 19