简体   繁体   中英

Can't set custom BackgroundColor on UITableViewCell

I'm using this code to customize the colour of my cell's background view:

UIColor *cellBackgroundColor = [UIColor colorWithRed:255.0 green:255.0 blue:255.0 alpha:1.0];
cell.contentView.backgroundColor = cellBackgroundColor;

The thing is, it doesn't work. This code though, works perfectly fine:

cell.contentView.backgroundColor = [UIColor redColor];

What's the problem here? Can't I customize the colour of a cell to my liking? Or am I doing something wrong?

Just change :

UIColor *cellBackgroundColor = [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0];
cell.contentView.backgroundColor = cellBackgroundColor;

you have to set Cell Background with this Delegate:-

- (void)tableView: (UITableView*)tableView
  willDisplayCell: (UITableViewCell*)cell
forRowAtIndexPath: (NSIndexPath*)indexPath
{

   // UIImage *img = [UIImage imageNamed:@"button.png"];  // get your background image

    UIColor *cellBackgroundColor = [UIColor colorWithRed:255/255.0 green:108/255.0 blue:61/255.0 alpha:1.0]; //if you want to set color then use this line

  //  UIColor *backgroundColor = [[UIColor alloc] initWithPatternImage: img];
    cell.backgroundColor = cellBackgroundColor;
    cell.textLabel.backgroundColor = [UIColor clearColor];
    cell.detailTextLabel.backgroundColor = [UIColor clearColor];
}

Your table look like:-

在此处输入图片说明

Taking an RGB color and normalizing it with UIColor on the iPhone

Your values are between 0 and 255. Use them to create a UIColor:

    float r; float g; float b; float a;

    [UIColor colorWithRed:r/255.f
                    green:g/255.f
                     blue:b/255.f    
                    alpha:a/255.f];
cell.backgroundView.backgroundColor

you can make a custom view and can change the complete backgroundview

UIView *backgroundViewForCell = [[[UIView alloc] init] autorelease];
backgroundViewForCell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"row-bg.png"]];
UIView *selectedBackgroundViewForCell = [[[UIView alloc] init] autorelease];
selectedBackgroundViewForCell.backgroundColor = [[UIColor blueColor] colorWithAlphaComponent:.2];
cell.backgroundView = backgroundViewForCell;
cell.selectedBackgroundView = selectedBackgroundViewForCell;

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.

Other LOgic IS

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];

you are on the right track. You just need to replace your color code with this

UIColor *cellBackgroundColor = [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0];

Enjoy Programming!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM