Steaphann
Steaphann

Reputation: 2777

changing the background color of each other cell in objective C

I have a uiTableView. I want that each other cell gets another background color. I am trying it like this. But it won't work. Anybody have a clue?

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

Kind regards,

Stef

Upvotes: 3

Views: 6545

Answers (3)

Igor N
Igor N

Reputation: 381

To answer the expanded question in the comments, set the background colour of your labels to label.backgroundColor = [UIColor clearColor];

Upvotes: 0

Lithu T.V
Lithu T.V

Reputation: 20021

cell.backgroundView=[[UIView alloc]initWithFrame:cell.frame];
[cell.backgroundView setBackgroundColor:[UIColor blueColor]];

will do the job or

[cell.contentView setBackgroundColor:[UIColor blueColor]];

Upvotes: 0

Moxy
Moxy

Reputation: 4212

You can use : cell.contentView.backgroundColor instead of cell.backgroundColor inside tableView:cellForRowAtIndexPath:

Or : (from Apple docs)

Note: If you want to change the background color of a cell (by setting the background color of a cell via the backgroundColor property declared by UIView) you must do it in the tableView:willDisplayCell:forRowAtIndexPath: method of the delegate and not in tableView: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.

Upvotes: 7

Related Questions