MAC开发--自定义NSTableView选中行背景

NSTableView的默认选中行颜色是蓝色的背景色,这就不可避免得涉及到自定义NSTableView选中行背景的需求。接下来我就介绍一下两种解决方法,以及需要注意的地方。

方法一:继承NSTableRowView

1、新建一个NSTableRowView的子类,重写- (void)drawSelectionInRect:(NSRect)dirtyRect的方法:

//直接改变点击背景色的方法
- (void)drawSelectionInRect:(NSRect)dirtyRect
{
    if (self.selectionHighlightStyle != NSTableViewSelectionHighlightStyleNone) {
        
        NSRect selectionRect = NSInsetRect(self.bounds, 1, 1);
        
        [[NSColor colorWithWhite:0.9 alpha:1] setStroke];  //设置边框颜色
        [[NSColor redColor] setFill];  //设置填充背景颜色
        
        NSBezierPath *path = [NSBezierPath bezierPathWithRect:selectionRect];
        [path fill];
        [path stroke];
    }
}
//点击cell呈现自定义图片的方法
- (void)drawSelectionInRect:(NSRect)dirtyRect
{
    if (self.selectionHighlightStyle != NSTableViewSelectionHighlightStyleNone)
    {
        
        NSImage *image = [NSImage imageNamed:@"选中"];
        NSImageRep *imageRep = image.representations[0];
        NSRect fromRect = NSMakeRect(0, 0, imageRep.size.width, imageRep.size.height);
        [imageRep drawInRect:dirtyRect fromRect:fromRect operation:NSCompositingOperationSourceOver fraction:1.0 respectFlipped:YES hints:nil];
    }
}

2、实现协议NSTableViewDelegate的- (NSTableRowView )tableView:(NSTableView )tableView rowViewForRow:(NSInteger)row代理方法:

//指定自定义的行
- (nullable NSTableRowView *)tableView:(NSTableView *)tableView rowViewForRow:(NSInteger)row
{
    CustomTableRowView *rowView = [tableView makeViewWithIdentifier:@"rowView" owner:self];
    if (!rowView) {
        
        rowView = [[CustomTableRowView alloc] init];
        rowView.identifier = @"rowView";
    }
    return rowView;
}

3、注意事项:
①、最好不要在控制器中设置tableView的selectionHighlightStyle。如果设置为NSTableViewSelectionHighlightStyleNone,不会有点击效果;如果设置为NSTableViewSelectionHighlightStyleRegular,可以正常显示自定义的背景;如果设置为NSTableViewSelectionHighlightStyleSourceList,drawSelectionInRect的方法将不会执行。所以还是建议既然你想自定义背景,就不要在在控制器中设置tableView的selectionHighlightStyle了。
②、不要在自定义NSTableCellView的子类中设置self.layer.backgroundColor,这个属性也将导致自定义的背景不生效。

方法二:继承NSTableCellView

1、新建一个NSTableCellView的子类,重写- (void)setBackgroundStyle:(NSBackgroundStyle)backgroundStyle方法:

-(void)setBackgroundStyle:(NSBackgroundStyle)backgroundStyle
{
    [super setBackgroundStyle:backgroundStyle];
    if(backgroundStyle == NSBackgroundStyleDark)
    {
        self.layer.backgroundColor = [NSColor yellowColor].CGColor;
    }
    else
    {
        self.layer.backgroundColor = [NSColor whiteColor].CGColor;
    }
}

2、注意事项:
①、这个方法不能设置自定义的图片
②、这个方法不能设置边框的颜色

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,645评论 25 708
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,009评论 19 139
  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 9,093评论 3 38
  • 又是一年高考时,15年了😓,我都快要忘记曾经为高考奋斗的自己,太久远了,只记得第一次高考失利时,父亲拿起板...
    9e184e6df859阅读 256评论 0 0
  • /01/ 近一年以来睡眠都及其好,好到我严重怀疑自己得了嗜睡症。从小就知道失眠是一种什么感觉,如今听到失眠两个字恍...
    路汐同学阅读 263评论 0 1