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.