简体   繁体   中英

Grouped UITableView custom draw cell, without changing background color?

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

UIColor * background = [self.superview backgroundColor];

I think calling [super drawRect:rect]; at the top of your custom draw rect will fix this problem

你有没有尝试过?

 [[UIColor clearColor] set];

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