GarryO
GarryO

Reputation: 75

drawRect doesn't reflect changes made to layer

The UITableView I am using has a custom UItableViewCell. This custom cell has a subview (an UIView subclass) to it. I use the drawRect of the custom UIView subclass to place all the text to be displayed by the cell.

And in the drawRect (of UIView subclass) I do the following

/*
// This piece of code is called when setNeedsDisplay is called
*/
- (void)drawRect:(CGRect)rect
{

    self.layer.cornerRadius = 10.0f;
    self.layer.backgroundColor = [[UIColor orangeColor] CGColor];
    self.layer.borderColor = [[UIColor lightGrayColor] CGColor];
    self.layer.borderWidth = 3.0f;
}

However my custom cell is a black square like this

enter image description here

But I do see the intended behavior if I select the row. Like shown below

enter image description here

Whats going on ?

Upvotes: 4

Views: 481

Answers (4)

tikamchandrakar
tikamchandrakar

Reputation: 92

You are doing one mistake:

Please go to the view in side the UItableViewCell and check the background color it may be black or something others, Reset it to clear color then check your result,

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726539

Your drawRect: method does not draw anything; the code that you put in there belongs in your initWithFrame: implementation.

You should manipulate the layer configuration in the initializer; in your drawRect: you should call functions of your CGContextRef based on the state of the view. For example, to draw some text you would use CGContextShowTextAtPoint, to draw some lines you would use CGContextAddLineToPoint, and so on.

See this question for information on the relationship between drawRect: and the CALayer of your UIView.

Upvotes: 3

Kai Huppmann
Kai Huppmann

Reputation: 10775

Try to set self.layer.masksToBounds = YES and (maybe) self.opaque = NO during your UIView's (the one where drawRect is overridden) initialization. (see this question)

Upvotes: 0

Scar
Scar

Reputation: 3480

Try to disable the selection highlight of the cell by using

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

Upvotes: 0

Related Questions