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.
source