4

I have a UITableViewCell subclass with the following drawRect: implementation. It will draw a line at the bottom of the cell, indented by 30 points to match our design. The tableView.separatorStyle is set to UITableViewSeparatorStyleNone in lieu of this custom drawing.

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];

    if (!_hideBottomLine) {
        CGContextRef ref = UIGraphicsGetCurrentContext();
        CGContextSetShouldAntialias(ref, NO);

        CGContextSetStrokeColorWithColor(ref, [UIColor colorWithWhite:0.75 alpha:1].CGColor);
        CGContextSetLineWidth(ref, 1);
        CGContextMoveToPoint(ref, 30, CGRectGetMaxY(rect));
        CGContextAddLineToPoint(ref, CGRectGetMaxX(rect), CGRectGetMaxY(rect));
        CGContextStrokePath(ref);
    }
}

This worked great in iOS 6 and on iOS 7 when built with the iOS 6 SDK. Now that I am building with the iOS 7 SDK the line does not appear.

Am I missing some change with CG drawing in the iOS 7 SDK?

EDIT:

So I now realize there is a better way to do this in iOS 7 using cell.separatorInset, I also found some other similar CG code I had written that works. So I think the issue is isolated to implementing drawRect: on UITableViewCell

I'd still like an answer on how to do custom drawing on the cell in iOS 7 though.

Shankar BS
  • 8,394
  • 6
  • 41
  • 53
Chris Wagner
  • 20,773
  • 8
  • 74
  • 95
  • Are those lines within the cell limit? iOS7 has a new subview that clips to bounds anything you add to the contentView. – Bms270 Sep 29 '13 at 04:40
  • These specifically are on the edge, so it's possible they're getting clipped as you say. However, I use the same technique in another cell where I draw a vertical line in the center of the cell which is not displaying in iOS 7. – Chris Wagner Sep 29 '13 at 20:03
  • Just try to remove the hidden clipping mechanism in iOS7 and see what happens: [cell.contentView.superview setClipsToBounds:NO]; – Bms270 Oct 01 '13 at 19:59

2 Answers2

3

Try set background color property to transparent color

self.backgroundColor = [UIColor clearColor]
Antaresm
  • 580
  • 2
  • 5
  • 19
  • So strange. This made the drawing appear in the cell. May you kindly explain why? – Houman Nov 10 '13 at 15:29
  • I think, custom draw make image in background view, but cell have content view and if you not set clear color, content view will be showing under your custom background – Antaresm Nov 28 '13 at 21:00
  • 1
    In iOS 7 the default background view is white. Before it was clearColor. – MartinMoizard Jan 29 '14 at 14:38
2

You must create subview with drawRect and put it to your UITableViewCell

look here Can't draw in UITableViewCell's drawRect

Community
  • 1
  • 1
Antaresm
  • 580
  • 2
  • 5
  • 19