Reputation: 1951
I have written drawRect as follows.
-(void)drawRect:(CGRect)rect
{
[super drawRect:rect];
CGContextRef cxt = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(cxt, 2.0);
CGContextSetStrokeColorWithColor(cxt, [UIColor redColor].CGColor);
CGContextMoveToPoint(cxt, 250.0 , 0.0);
CGContextAddLineToPoint(cxt, 250.0, 50.0);
CGContextStrokePath(cxt);
}
It draws red line. But When I set background view to cell line disappears. I have set view as follow.
UIView *view = [[UIView alloc] initWithFrame:cell.frame];
view.backgroundColor = [UIColor grayColor];
cell.backgroundView = view;
What is the problem? How backgrond view hides the line? Please help
Upvotes: 2
Views: 1719
Reputation: 381
When u already drawn background of that,u can't set background Color or image again for that cell.It overlays on what u drawn.
Upvotes: 0
Reputation: 4652
Agree with jaydee3 that you should not override UITableViewCell's drawRect, instead make your custom view class extending from UIView, extend drawRect there and do all the framing and coloring thing there, then set an instance of that view as your cell background view, its much better approach
Upvotes: 0
Reputation: 9977
I guess you are in a UITableViewCell
?
You should not overwrite drawRect
of the cell itself. Instead put your drawing code in a custom backgroundView
, or in a custom view within the contentView
hierarchy. (depends on your planned result, probably backgroundView is correct for you)
The line is gone, because the backgroundView
is a subview of the TableViewCell, so it is on top of the cell itself. (You can see this, if you use [UIColor colorWithWhite:0.0 alpha:0.5]
as backgroundColor of your backgroundView.) There are many views on a UITableViewCell, it looks somewhat like this:
UITableViewCell
> BackgroundView
> (EditControl)
> ContentView
> (AccessoryView)
Upvotes: 1