objective c - View-based NSTableView selection? -
i wrote view-based tableview, this:
and draw selection nstablerowview, code this:
- (void)drawrect:(nsrect)dirtyrect { [[nscolor clearcolor] setfill]; if (self.isclicked) { [[[nscolor blackcolor] colorwithalphacomponent:0.08] setfill]; } nsrect rowviewrect = nsmakerect(0, 0, 274, 72); nsbezierpath *path = [nsbezierpath bezierpathwithrect:rowviewrect]; [path fill]; }
but finally, found tablerowview not on tableview, selectedcolor not cover image , button, more background color, need selected tablerowview cover view, this:
the selected color cover image , button. have googled, not found ideas. help~
so bit tricky. strategy have overlay colored view alpha less 1 in nstablecellview , add , remove based on cell's selection.
first, need nsview can set background color:
nsview_background.h
@interface nsview_background : nsview @property (nonatomic, strong) nscolor *background; @end
nsview_background.m
#import "nsview_background.h" @implementation nsview_background - (void)drawrect:(nsrect)dirtyrect { [self.background set]; nsrectfill([self bounds]); } - (void)setbackground:(nscolor *)color { if ([_background isequal:color]) return; _background = color; [self setneedsdisplay:yes]; } @end
second, in nstablecellview subclass, add nsview_background property:
#import "nsview_background.h" @interface @property (nonatomic, strong) nsview_background *selectionview; @end
third, add method nstablecellview subclass:
- (void)shouldshowselectionview:(bool)shouldshowselectionview { if (shouldshowselectionview) { self.selectionview = [[nsview_background alloc] init]; [self.selectionview setbackground:[nscolor graycolor]]; self.selectionview.alpha = 0.4; [self addsubview:self.selectionview]; [self setneedsdisplay:yes]; // draws selection view } else { [self.selectionview removefromsuperview]; self.selectionview = nil; } }
fourth, add drawrect in nstablecellview subclass:
- (void)drawrect:(nsrect)dirtyrect { if (self.selectionview) self.selectionview.frame = self.bounds; }
finally, override nstablecellview:setbackgroundstyle:
- (void)setbackgroundstyle:(nsbackgroundstyle)backgroundstyle { switch (backgroundstyle) { case: nsbackgroundstyledark: [self shouldshowselectionview:yes]; break; default: [self shouldshowselectionview:no]; break; } }
i know seems hacky way behavior. hope helps , luck!
Comments
Post a Comment