I want to achieve the effect where one table cell will have a blue background, the next will be white, the next will be blue again, and then white, and so on … can you please let me know how can I do this?

Thank you.

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]; } 

You have to set the background color of the cell content content.

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

This will set the background of the entire cell.

To this end, for alternate use of cells indexPath.rowand% by 2.

If you want to set the color of the cells based on some state in the actual cell data object, then this is a different 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:

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

This saves you the trouble of fetching your data objects again in the willDisplayCell method.

The method cell.contentView.backgroundColor = [UIColor colorWithRed…] in the lostInTransit answer works if you are not using the UITableViewCell inline label.

I found that if you use an inline label, such as setting cell.text, you get an opaque white box under the label and only two ends of the cell display the color you want.

I haven’t found a way to edit the inline label to be opaque (you can access it via UILabel* cellLabel = [cell.contentView.subviews objectAtIndex:0]).

I solved the problem by adding my own 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];

add the following code to cellForRowAtIndexPath

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

i think this will help you

When using the default table cell setting, the following two properties will color both the background of the cell and the background color of its label, avoiding the need to create a custom label as a peek of the cell’s content View.

 cell.textLabel.backgroundColor = [UIColor redColor]; cell.contentView.backgroundColor = [UIColor redColor]; 

// time saving method:

// in the cell for the 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 contents of the cell and then return it. Easy work.

// this is the schema code. Don’t copy it directly.

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, as mentioned earlier based on indexPath value% 2, you can execute the above function.

This code is a slightly cleaner solution for the case where you are using an inline text label and do not want the white background color of the text label to overlap the background color. This also fixes the problem of breaking the rounded corners of the grouped table view style (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
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];

All you need to do is add the following code to the table 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 as easy as adding and changing module values.

Add backgroundViewto cell and set the 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 

Leave a Reply

Your email address will not be published. Required fields are marked *