Using Core Graphics to draw a separator line on top of standard grouped tableView cell

I can't believe I am asking the classic "How do I draw a line" question, but it is a little more complicated than that.

I have a grouped tableview whose separatorColor I have set to clear. This removes the border and separators. I also have a category on UITableViewCell that I am using to draw some gradients around the cells.

I would like to be able to also draw line separators within this same category. Here is what I have so far:

CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSaveGState(ctx);

float y = height;

CGContextMoveToPoint(ctx, CGRectGetMinX(rect), y);
CGContextAddLineToPoint(ctx, CGRectGetMaxX(rect), y);

CGContextSetStrokeColorWithColor(ctx, color.CGColor);
CGContextSetLineWidth(ctx, width);
CGContextStrokePath(ctx);

CGContextRestoreGState(ctx);

This works, but the line shows up behind the tableView cells. I would like for it to be visible on top of the cells.

What am I missing?

Thanks!

EDIT: Screenshot

If you look really closely, you can see green pixels on the edges. The bottom one is completely visible.

EDIT 2: Code

- (void)drawRect:(CGRect)rect {
     CGContextRef ctx = UIGraphicsGetCurrentContext();
     CGContextSaveGState(ctx);

     [self drawLineSeparator:self.contentView.frame];
}


- (void) drawLineSeparator:(CGRect)rect {
     [self drawLineAtHeight:CGRectGetMaxY(rect)
                  rect:rect
                 color:[UIColor colorWithRed:0 green:1 blue:0 alpha:.7]
                 width:1];
}


- (void) drawLineAtHeight:(float)height rect:(CGRect)rect color:(UIColor *)color width:(float)width {
     CGContextRef ctx = UIGraphicsGetCurrentContext();
     CGContextSaveGState(ctx);

     float y = height;

     CGContextMoveToPoint(ctx, CGRectGetMinX(rect), y);
     CGContextAddLineToPoint(ctx, CGRectGetMaxX(rect), y);

     CGContextSetStrokeColorWithColor(ctx, color.CGColor);
     CGContextSetLineWidth(ctx, width);
     CGContextStrokePath(ctx);

     CGContextRestoreGState(ctx);
}

Answers


First, it's a bad idea to override methods of framework classes using a category. What you have done affects every instance of UITableViewCell in your app. You aren't directly responsible for every table view cell in your app! For example, a UIPickerView has embedded table views, and a UIDatePicker has an embedded UIPickerView. So if you use either of those, your category may change their appearance in ways you didn't expect or want.

Instead, create a subclass of UITableViewCell and override drawRect: in your subclass.

Second, UITableViewCell uses a subview to draw its background. A subview's contents is always “on top of” the superview's contents. So your green line is underneath the background view's contents. That's why you can't see it.

One fix is to simply add a one-point-tall, green subview to the cell. Then you don't have to override drawRect: at all. You can even do it in your cell subclass. Example:

// MyCell.h

@interface MyCell : UITableViewCell
@end

// MyCell.m

#import "MyCell.h"

@implementation MyCell {
    UIView *greenLineView;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    [self layoutGreenLineSubview];
}

- (void)layoutGreenLineSubview {
    if (!greenLineView) {
        greenLineView = [[UIView alloc] init];
        greenLineView.backgroundColor = [UIColor greenColor];
        greenLineView.userInteractionEnabled = NO;
        [self.contentView addSubview:greenLineView];
    }
    CGRect frame = self.contentView.bounds;
    frame.origin.y = CGRectGetMaxY(frame);
    frame.size.height = 1;
    greenLineView.frame = frame;
}

@end

Need Your Help

convert date data into mysql date format

php mysql date

I want to convert the data on which I have the format

How to escape single quote

php arrays postgresql fetch

i m using pg_query_params function to add values in my table vmobjects in my addvm.php page.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.