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.
Background Color for Table Cell. You can also change the background color of an individual table cell. To do this, simply apply the styles against the <td> tag of the table cell in question.
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'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.
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