开发者

Changing background color of a custom UITableViewCell cell in a plain styled UITableView

开发者 https://www.devze.com 2023-03-18 00:28 出处:网络
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:

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

0

精彩评论

暂无评论...
验证码 换一张
取 消