4

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 ?

GarryO
  • 75
  • 4

4 Answers4

3

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.

Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • So does that mean, if I don't draw anything in drawRect, the initializations i had done in initWithFrame don't show ? – GarryO Sep 18 '12 at 17:06
  • @GarryO If you do not draw anything in `drawRect:`, you should remove the method; otherwise, your rectangle will remain black. – Sergey Kalinichenko Sep 18 '12 at 17:14
0

Try to disable the selection highlight of the cell by using

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
Scar
  • 3,460
  • 3
  • 26
  • 51
0

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)

Community
  • 1
  • 1
Kai Huppmann
  • 10,705
  • 6
  • 47
  • 78
0

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,