iphone - Using Core Graphics to draw a separator line on top of standard grouped tableView cell -
i can't believe asking classic "how draw line" question, little more complicated that.
i have grouped tableview separatorcolor have set clear. removes border , separators. have category on uitableviewcell using draw gradients around cells.
i able draw line separators within same category. here have 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, line shows behind tableview cells. visible on top of cells.
what missing?
thanks!
edit: screenshot
if closely, can see green pixels on edges. bottom 1 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); }
first, it's bad idea override methods of framework classes using category. have done affects every instance of uitableviewcell
in app. aren't directly responsible every table view cell in app! example, uipickerview
has embedded table views, , uidatepicker
has embedded uipickerview
. if use either of those, category may change appearance in ways didn't expect or want.
instead, create subclass of uitableviewcell
, override drawrect:
in subclass.
second, uitableviewcell
uses subview draw background. subview's contents “on top of” superview's contents. green line underneath background view's contents. that's why can't see it.
one fix add one-point-tall, green subview cell. don't have override drawrect:
@ all. can in 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
Comments
Post a Comment