why does setting a UITableViewCell
's color work in willDisplayCell
but not in cellForRowAtIndexPath
?
Notes:
[cell setBackgroundColor:[UIColor yellowColor]];
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
The doco for UITableViewCell
states that you need to do it in willDisplayCell:
(excerpt from the UITableViewCell
class reference below). I suspect that because table cells have a content view and various other components that seem to be 'managed' by UIKit
, that its doing its own thing with the background color after its set in cellForRowAtIndexPath
. You can also use the 'backgroundView' property of a table view cell to control its background.
documentation excerpt:
Note: If you want to change the background color of a cell (by setting the background color of a cell via the backgroundColor
property declared by UIView
) you must do it in the tableView:willDisplayCell:forRowAtIndexPath:
method of the delegate and not in tableView:cellForRowAtIndexPath:
of the data source. Changes to the background colors of cells in a group-style table view has an effect in iOS 3.0 that is different than previous versions of the operating system. It now affects the area inside the rounded rectangle instead of the area outside of it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With