Changing the background color in a custom UITableViewCell cell in the normal UITableView style

I am trying to change the background color of a user UITableViewCellin the normal UITableView style.

I read other similar questions, and I'm in my delegate method:

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath

I have installed cell.backgroundColor = [UIColor...]. The problem is that this only works for a grouped style UITableView.

Note. The reason I want to do this programmatically, and not in my .xib file, is because I want different background colors for different cells.

+5
source share
3 answers

Perhaps you can add cell.contentView.backgroundColor = [UIColor clearColor]before setting the color.

, . .

+8

:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (...){
        cell.backgroundColor = [UIColor blueColor];
    } else {
        cell.backgroundColor = [UIColor whiteColor];
    }
}
+1
// set selection color
UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame];

myBackView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:0.75 alpha:1];

[cell.contentView addSubview:myBackView];

[myBackView release];

how this will change each cell as needed

0
source

All Articles