Changing background color of a custom UITableViewCell cell in a plain styled UITableView
I am trying to change the background color of a custom UITableViewCell
in a plain style UITableView.
I've read the other similar questions, and I in my delegate method:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
I set cell.backgroundColor = [UIColor...]
. The problem is this only 开发者_JAVA技巧works for a grouped style UITableView
.
Note: The reason why I want to do this programmatically and not in my .xib file is because I want different background colors for different cells.
Maybe you can add cell.contentView.backgroundColor = [UIColor clearColor]
before you set your color.
The reason is that the backgroundview is at the bottom. The contentview is at the top.
Change the color in the following delegate method:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if (...){
cell.backgroundColor = [UIColor blueColor];
} else {
cell.backgroundColor = [UIColor whiteColor];
}
}
// 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];
like this change every cell as u needed
精彩评论