0

I want change background of all cells in a tableView, I have used initWithStyle:UITableViewCellStyleValue1 how type of cell, (because I want this type but background is white) I do not want to change the background of Tableview but only the cell, is this possible?

WilliamKF
  • 41,123
  • 68
  • 193
  • 295
Doom
  • 1,258
  • 3
  • 15
  • 24
  • Here's [a simpler answer and much more](http://stackoverflow.com/questions/281515/how-to-customize-the-background-color-of-a-uitableviewcell/2803123#2803123). (1-st result from [Google](http://www.google.com/search?q=uitableviewcell+background), btw ;) ) – adubr May 17 '11 at 01:31

1 Answers1

2

That style of table cell consists of two labels, one with the main text on the left, and a second with blue text on the right.

Try setting the background colors of the label cells:

tableCell.textLabel.backgroundColor = ...
tableCell.detailTextLabel.backgroundColor = ...

Edit: You also need to set the background color of the tableCell's contentView.

tableCell.contentView.backgroundColor = ...

Edit 2: The following code also ensures that the background color extends to the accessory

UIColor *bgColor = [UIColor greenColor];
tableCell.textLabel.text = @"abc";
tableCell.textLabel.backgroundColor = bgColor;

tableCell.detailTextLabel.text = @"def";
tableCell.detailTextLabel.backgroundColor = bgColor;

tableCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

tableCell.backgroundView = [[[UIView alloc] init] autorelease];
tableCell.backgroundView.backgroundColor = bgColor;
jjwchoy
  • 1,898
  • 16
  • 20
  • i have use: cell.contentView.backgroundcolor... but for change color to cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; how can make? – Doom May 16 '11 at 18:57
  • AFAIR, you can throw away everything above the last two lines from the last code snippet – adubr May 17 '11 at 01:34
  • I've tried this and if you don't set the background colors of the textLabel and detailLabel then they will just be white. – jjwchoy May 17 '11 at 01:44