I am custom drawing a table cell in a grouped cell. How do I get the background color right?
I actually have a view inside the cell, and am customizing how that view is drawn in its drawRect:.
I tried this:
- (void)drawRect:(CGRect)rect
{
BOOL isHighlighted = self.highlighted;
CGRect frame = self.bounds;
CGContextRef c = UIGraphicsGetCurrentContext();
if ( !isHighlighted ) {
[[UIColor whiteColor] set];
CGContextFillRect( c, frame );
}
// rest of drawing
}
This works well with the plain table view, but it isn’t a match for the grouped table view. It looks like about a 96% grey.
It looks like this:

If your screen isn’t calibrated well, you might not see the difference. But it’s there.
When I tried this:
if ( !isHighlighted ) {
//[[UIColor whiteColor] set];
//CGContextFillRect( c, frame );
}
I instead got a black background, like this:

My assumption is that I need to fill every pixel in my draw routine.
When I try this:
if ( !isHighlighted ) {
[[self backgroundColor] set];
CGContextFillRect( c, frame );
}
I also get the black background. I think the backgroundColor of my view is transparentColor.
I’ve tried this, too:
if ( !isHighlighted ) {
CGContextFillRect( c, frame );
}
Same black box.
How do I match the background color of a grouped table view, without using the eyedropper and hard-coding a [UIColor colorWithWhite: alpha:] into my app?
get the background colour of its super view
I think calling
[super drawRect:rect];at the top of your custom draw rect will fix this problem