2016-04-25 101 views
0

我写了一个基于视图的tableview中,像这样: enter image description here基于视图的NSTableView选择?

,我画的选择与NSTableRowView,代码是这样的:

- (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]; 
} 

但最后,我发现TableRowView还没有结束的tableView ,所以selectedColor没有覆盖图像和按钮,它更像是背景颜色,但我需要选择TableRowView覆盖视图,就像这样:

enter image description here

所选颜色覆盖图像和按钮。我GOOGLE了,但没有发现任何想法。感谢您的帮助〜

回答

1

所以这有点棘手。策略是在NSTableCellView中使用alpha小于1的叠加彩色视图,然后根据单元的选择来添加和删除它。

首先,你需要,可以设置背景颜色的的NSView:

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 

,在你NSTableCellView子类,添加NSView_Background属性:

#import "NSView_Background.h" 

@interface 
@property (nonatomic, strong) NSView_Background *selectionView; 
@end 

,这种方法添加到NSTableCellView子类:

- (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 the selection view 
    } else { 
     [self.selectionView removeFromSuperview]; 
     self.selectionView = nil; 
    } 
} 

,在你NSTableCellView子类这增加的drawRect:

- (void)drawRect:(NSRect)dirtyRect { 
    if (self.selectionView) 
     self.selectionView.frame = self.bounds; 
} 

Final LY,覆盖NSTableCellView:setBackgroundStyle:

- (void)setBackgroundStyle:(NSBackgroundStyle)backgroundStyle { 
    switch (backgroundStyle) { 
     case: NSBackgroundStyleDark: 
      [self shouldShowSelectionView:YES]; 
      break; 
     default: 
      [self shouldShowSelectionView:NO]; 
      break; 
    } 
} 

我知道这似乎哈克,但这是我能得到这种行为的唯一途径。希望这会有所帮助,祝你好运!

+0

完美,真的非常感谢您的帮助。 – melody5417

+0

如果这回答你的问题,请接受这个答案。 – rocky

+0

对不起,但我已经接受了这个答案。我点击了uparrow,那么这个答案将被标记为接受,对吗?对不起,如果这不是正确的方法,我会纠正它。 – melody5417

相关问题