DrawRect does not reflect changes made to the layer

UITableView I have a custom UItableViewCell . This custom cell has a preview (subclass of UIView). I use the drawRect custom UIView subclass to place all the text that the cell will display.

And in drawRect (subclass of UIView ) 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 see intentional behavior if I select a row. As below

enter image description here

What's happening?

+4
source share
4 answers

Try setting self.layer.masksToBounds = YES and (possibly) self.opaque = NO during your UIView (the one where drawRect overridden) initialization. (see this question )

0
source

Your drawRect: method drawRect: draws nothing; the code you entered there belongs to your initWithFrame: implementation initWithFrame:

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

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

+3
source

Try disabling cell selection highlighting with

 [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 
0
source

You make one mistake:

Please go to the UItableViewCell side view and check the background color, which may be black or something else, Reset to clear the color, and then check your result.

0
source

Source: https://habr.com/ru/post/1434894/


All Articles