35

I want to achieve the effect where one cell of the table view will have blue background, the next one will have white, the next one will have blue again, and then white and so on... could you let me know how can I do that?

Thanks.

ebaccount
  • 5,051
  • 11
  • 43
  • 47
  • possible duplicate: http://stackoverflow.com/questions/281515/how-to-customize-the-background-color-of-a-uitableviewcell – ilhnctn Jul 10 '12 at 09:31
  • 1
    @willcodejavaforfood that is a very unhelpful comment and is not relevant to the OPs question – Max May 15 '15 at 13:14

12 Answers12

73

Add this method to your table view delegate:

#pragma mark UITableViewDelegate
- (void)tableView: (UITableView*)tableView 
  willDisplayCell: (UITableViewCell*)cell 
forRowAtIndexPath: (NSIndexPath*)indexPath
{
    cell.backgroundColor = indexPath.row % 2 
        ? [UIColor colorWithRed: 0.0 green: 0.0 blue: 1.0 alpha: 1.0] 
        : [UIColor whiteColor];
    cell.textLabel.backgroundColor = [UIColor clearColor];
    cell.detailTextLabel.backgroundColor = [UIColor clearColor];
}
jessecurry
  • 22,068
  • 8
  • 52
  • 44
  • 3
    +1 This worked and was the only way I can get the textLabel.backgroundColor setting to stick. Setting it in tableView:cellForRowAtIndexPath: didn't work for me (OS 3.2) but this did. – progrmr May 08 '10 at 21:41
  • 2
    There is an old WWDC video from 2009 or 2010 which covers this. The table view will adjust the background to manage the cells' selection states, which is why the only place you can modify it reliably is the willDisplayCell method. – Mike Weller May 07 '12 at 13:43
  • does anyone understand why setting this in cellforrowatindex doesn't work properly? I tried in that method, but %2 doesn't do it properly for every other row. And when you scroll up and down a few times, the background on each cell changes. willDisplayCell works though, just wondering why doesn't work for cellforrow – khanh.tran.vinh Jul 16 '13 at 16:22
  • See Mike Weller's answer above – jessecurry Jul 16 '13 at 18:51
43

You have to set the background color of the cell's content view

cell.contentView.backgroundColor = [UIColor colorWithRed...]

This will set the background of the whole cell.

To do this for alternate cells, use the indexPath.row and % by 2.

Navnath Godse
  • 2,233
  • 2
  • 23
  • 32
lostInTransit
  • 70,519
  • 61
  • 198
  • 274
  • Thanks for your reply. I have tried that but it only shows two ends of the cell as blue/orange, but I wanted the whole cell to have the color – ebaccount May 25 '09 at 17:05
  • 2
    You will get the ends as blue/orange if you use cell.backgroundColor. If you use cell.contentView.backgroundColor, the whole cell will be colored, unless you have any controls in your cell (like a label) which have a white background. In that case, you'll have to change their background color too. Otherwise the contentView.backgroundColor method has worked for me till now. – lostInTransit May 26 '09 at 06:12
  • i came here looking for how to change the background color of whole cell, however the question posted is not exactly related but this answer worked for me, Thanks – Sarim Sidd Apr 28 '13 at 08:58
  • just added one more line "cell.backgroundColor = cell.contentView.backgroundColor; " Thanks – Sarim Sidd Apr 28 '13 at 09:12
9

If you want to set cell color based on some state in the actual cell data object, then this is another approach:

If you add this method to your table view delegate:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = cell.contentView.backgroundColor;
}

Then in your cellForRowAtIndexPath method you can do:

if (myCellDataObject.hasSomeStateThatMeansItShouldShowAsBlue) {
    cell.contentView.backgroundColor = [UIColor blueColor];
}

This saves having to retrieve your data objects again in the willDisplayCell method.

gamozzii
  • 3,911
  • 1
  • 30
  • 34
  • 1
    I found that this approach also avoids having to mess with the text label backgrounds - the tableView seems to adjust them automatically to match the cell backgroundColor. For my use case, this is the simplest solution among the answers presented here. – David Ravetti Mar 29 '13 at 20:40
6

please add the following code in the cellForRowAtIndexPath

if (indexPath.row % 2 == 0){
    cell.backgroundColor =[UIColor blueColor];
} else {
    cell.backgroundColor =[UIColor whiteColor];
}

i think this will help you

Michael Mior
  • 28,107
  • 9
  • 89
  • 113
safil sunny
  • 106
  • 1
  • 5
4

The cell.contentView.backgroundColor = [UIColor colorWithRed...] method in lostInTransit's answer works, as long as you do not use the built-in label of a UITableViewCell.

I found that if you use the built-in label, e.g. by setting cell.text, you end up with a opaque white block under the label and only the two ends of the cell show your desired color.

I found no way to edit the built-in label so it is non-opaque (you can access it via UILabel* cellLabel = [cell.contentView.subviews objectAtIndex:0]).

I solved the problem by adding my own custom UILabel. Like this:

UILabel* cellLabel = [[[UILabel alloc] initWithFrame:cell.frame] autorelease];
cellLabel.text = @"Test with non-opaque label";
cellLabel.backgroundColor = [UIColor clearColor];
cellLabel.opaque = NO;

[cell.contentView addSubview:cellLabel];
cell.contentView.backgroundColor = [UIColor darkGrayColor];
henning77
  • 3,824
  • 2
  • 26
  • 32
  • You shouldn't create a label and change it's background color when you can just change the cell's background color. cell.backgroundColor = [UIColor ...] – Tony Lenzi Feb 19 '10 at 04:32
3

//time saving method:

//in cell for index method:

static NSString* evenIdentifier = @"even";
static NSString* oddIdentifier = @"odd";

__weak identifier;
if(indexPath.row %2 ==0)
{
    identifier = evenIdentifier;
}else{
    identifier = oddIdentifier;
}

cell = dequeue..WithIdentifier: identifier;
if(cell == nil){
    cell = allocOrLoadNib...;
    cell.backgroundColor = (indexPath.row %2 ==0 ? evenColor : oddColor);
}

// change the cell content and then return it. Easy job.

// this is a outline code. Please not copy it directly.

Guntis Treulands
  • 4,764
  • 2
  • 50
  • 72
Henry Sou
  • 882
  • 2
  • 12
  • 19
3

This worked for me .. In cellForRowAtIndexPath ,

cell.textLabel.backgroundColor = [UIColor clear];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
cell.contentView.backgroundColor = [UIColor grayColor]; //whichever color u want
cell.backgroundColor = cell.contentView.backgroundColor;

For you requirement, as mentioned earlier based on indexPath % 2 value you can perform the above function.

kirans_6891
  • 121
  • 9
3

When using the default table cell setting the following two properties will color both the cell's background and the background color of it's label, avoiding the need to create a custom label as a subview of the cell's contentView.

cell.textLabel.backgroundColor = [UIColor redColor];
cell.contentView.backgroundColor = [UIColor redColor];
hillmark
  • 807
  • 8
  • 22
  • This answer clarifies a few of the issues outlined in some of the other answers, but I don't have the necessary rating to comment on them. – hillmark Aug 18 '11 at 11:06
2

This code is a slightly more clean solution for the case where you are using the built-in text label and don't want the white background color of the text label obscuring the background color. This also fixes the issue of violating the rounded corners of a grouped style of table view (which happens when you use cell.contentView.backgroundColor instead of cell.backgroundColor) :

UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.backgroundColor = [UIColor redColor];
cell.textLabel.backgroundColor = [UIColor clearColor]; //transparent background
John M. P. Knox
  • 713
  • 4
  • 11
0
    UIView *bg = [[UIView alloc] initWithFrame:cell.frame];
    bg.backgroundColor = [UIColor colorWithRed:175.0/255.0 green:220.0/255.0 blue:186.0/255.0 alpha:1]; 
    cell.backgroundView = bg;
    [bg release];
Bhavesh Nayi
  • 3,626
  • 1
  • 27
  • 42
0

All you need to do is add the following code in your table's view controller implementation file.

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row%2 == 0) {
    UIColor *altCellColor = [UIColor blackColor];
    cell.backgroundColor = altCellColor;
}}

It's then as simple as adding and changing the modulus values.

0

Add backgroundView to the cell and set its background color of your choice.

let backgroundView = View() //  add background view via code or via storyboard
view.backgroundColor = UIColor.red // your color
cell.backgroundView = backgroundView
Satish
  • 2,015
  • 1
  • 14
  • 22