Hermann Klecker
Hermann Klecker

Reputation: 14068

Table Cell background and text color cannot be set accurately.

I need to set the background of table cells to a specific color. (#222222 or RGB(32,32,32) respectively)

The background of the table view in IB is set properly. The correct gray appears in the back of the table header and in section headers etc. But I struggle with the cells.

To customize the cell's apperance I subclass UITableCell and implement the layoutSubviews method.

This works fine:

- (void)layoutSubviews {

    [super layoutSubviews];

    self.selectionStyle                 = UITableViewCellSelectionStyleGray;

    self.backgroundColor                = [UIColor darkGrayColor];
    self.contentView.backgroundColor    = [UIColor darkGrayColor];
    self.textLabel.textColor            = [UIColor whiteColor];
    self.detailTextLabel.textColor      = [UIColor grayColor];
}

However, grayColor and darkGrayColor simply do not match the colour that I need.

Naturally I tried the colorWithRed:green:blue:alpha method of UIColor.

- (void)layoutSubviews {

    [super layoutSubviews];

    self.selectionStyle                 = UITableViewCellSelectionStyleGray;

    self.backgroundColor                = [UIColor colorWithRed:(32/256) green:(32/256) blue:(32/256) alpha:1];
    self.contentView.backgroundColor    = [UIColor colorWithRed:(32/256) green:(32/256) blue:(32/256) alpha:1];
    self.textLabel.textColor            = [UIColor whiteColor];
    self.detailTextLabel.textColor      = [UIColor colorWithRed:(32/256) green:(32/256) blue:(32/256) alpha:1];
}

That one results in black background and black color of the detailTextLable. (Of course it is senseless using the same color for a background and for a text label. I am just trying to work out what colorWithRed:green:blue:alpha does and does not.)

With plain style tables I am fine. Those' cells do not have a background color at all. When I just omit setting the backgroundColor and the contentView's background Color propierties then the background of the cells is displayed as defined as the Table's background color in IB. But with grouped tables the standard background is some light gray which I want to change to some more decent color that matches my client's style guide.

What am I doing wrong? Do I use colorWithRed:green:blue:alpha properly?

Any suggestion is much appreciated.

Upvotes: 0

Views: 1198

Answers (3)

Hermann Klecker
Hermann Klecker

Reputation: 14068

Thanks for putting your finger to the error.

However, this does not exactly answer the question related to the difference between plain and grouped table style.

Just in case somebody finds the question interesting:

  1. The grouped style comes with a background view. I set that to nil.
  2. The gropued style always has a cell background color set to something. Therefore omitting the self.backgroundColor=... statement was not sufficient.

finally

self.backgroundColor            = [UIColor clearColor];

did the trick. It was just not remotely connected to the objective-c mistake that I made too.

Upvotes: 0

Marcelo Alves
Marcelo Alves

Reputation: 1846

32/256 = 0 but 32/256.0 = 0.125.

Upvotes: 1

rckoenes
rckoenes

Reputation: 69479

I would try an other methods of calucaluting the color float:

[UIColor colorWithRed:(32.0f/255.0f) green:(32.0f/255.0f) blue:(32.0f/255.0f) alpha:1.0f];

since 0 is also include you have 0 to 255 values not 1 to 256 values.

If you want the cell to be transparent use [UIColor clearColor]

Upvotes: 1

Related Questions