why does setting a UITableViewCell‘s color work in willDisplayCell but not in cellForRowAtIndexPath?
Notes:
- I’m using this code to set the background color of a cell:
[cell setBackgroundColor:[UIColor yellowColor]]; - works when I put it in the method
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath - does NOT work if I put in in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
The doco for
UITableViewCellstates that you need to do it inwillDisplayCell:(excerpt from theUITableViewCellclass reference below). I suspect that because table cells have a content view and various other components that seem to be ‘managed’ byUIKit, that its doing its own thing with the background color after its set incellForRowAtIndexPath. 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
backgroundColorproperty declared byUIView) you must do it in thetableView:willDisplayCell:forRowAtIndexPath:method of the delegate and not intableView: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.