How is backgroundColor handled for UIView in Objective_C?

I played with some sample Apple code to configure UITableViewCells. I came across some weird behavior that completely confused me in how backgroundColor works.

The following code is a significantly reduced version of Apple's custom UIView example in a custom UITableViewCell. The init function sets the background color to purple, and then drawRect sets the background color to green. I expected to never see the purple color, but that’s all I see. Through NSLog, I know that the init method is called for each of the cells, and then drawRect is called for each of the cells. The green setting seems to be ignored. If I call [self setNeedsDisplay]at any time after the initial boot, the background will be correctly set to green.

- (id)initWithFrame:(CGRect)frame {
    counter = 0;
    if ((self = [super initWithFrame:frame])) {
        self.opaque = YES;
        self.backgroundColor = [UIColor purpleColor];
    }
    return self;
}

- (void)drawRect:(CGRect)rect { 
    self.backgroundColor = [UIColor greenColor];
}

Can someone explain to me why this is happening like this?

+3
source share
2 answers

-drawRect: , .

, , . , -drawRect:. , -drawRect: . , , , , -drawRect: .

, , , .

0

cellForRowAtIndexPath??

cell.textLabel.textColor = [UIColor purpleColor];
0

All Articles