Cocoa osx NSTableview change row highlight color

元气小坏坏 提交于 2019-12-07 03:46:17

问题


In my application I have a view based NSTableView with one column. The highlight color of the rows is set to regular (blue). I need to change that color to my custom color. In the interface builder I tried changing it but the only options are "None, regular and source list".

I tried this post solution with no success: https://stackoverflow.com/a/9594543/3065901

I read that I have to use this delegate method but I dont know how to use this.

- (NSTableRowView *)tableView:(NSTableView *)tableView rowViewForRow:(NSInteger)row

I tried drawing the row in that method but i get invalid context warnings and the row still keeps with the same higlight. Please post a simple example of how to use this delegate method:

Need help please. Thanks in advance.


回答1:


From this link.

MyNSTableRowView.h

#import <Cocoa/Cocoa.h>
@interface MyNSTableRowView : NSTableRowView
@end

MyNSTableRowView.m

#import "MyNSTableRowView.h"

@implementation MyNSTableRowView
- (id)init
{
    if (!(self = [super init])) return nil;
    return self;
}

- (void)drawSelectionInRect:(NSRect)dirtyRect {
     if (self.selectionHighlightStyle !=    NSTableViewSelectionHighlightStyleNone) {
     NSRect selectionRect = NSInsetRect(self.bounds, 2.5, 2.5);
     [[NSColor colorWithCalibratedWhite:.65 alpha:1.0] setStroke];
     [[NSColor colorWithCalibratedWhite:.82 alpha:1.0] setFill];
     NSBezierPath *selectionPath = [NSBezierPath bezierPathWithRoundedRect:selectionRect
                                                               xRadius:6 yRadius:6];
     [selectionPath fill];
     [selectionPath stroke];
     }
}
@end

AppDelegate.m

- (NSTableRowView *)tableView:(NSTableView *)tableView rowViewForRow:(NSInteger)row
{         
     MyNSTableRowView *rowView = [[MyNSTableRowView alloc]init];
     return rowView;
}



回答2:


Here is user3065901's answer in Swift 3:

class MyNSTableRowView: NSTableRowView {

    override func drawSelection(in dirtyRect: NSRect) {
        if self.selectionHighlightStyle != .none {
            let selectionRect = NSInsetRect(self.bounds, 2.5, 2.5)
            NSColor(calibratedWhite: 0.65, alpha: 1).setStroke()
            NSColor(calibratedWhite: 0.82, alpha: 1).setFill()
            let selectionPath = NSBezierPath.init(roundedRect: selectionRect, xRadius: 6, yRadius: 6)
            selectionPath.fill()
            selectionPath.stroke()
        }
    }
}

NSTableViewDelegate:

func tableView(_ tableView: NSTableView, rowViewForRow row: Int) -> NSTableRowView? {
    return MyNSTableRowView()
}



回答3:


If you are using cell based tableview, set the NSTableView selectionHighLightStyle to None NSTableView, and override drawRow sample below

- (void)drawRow:(NSInteger)row clipRect:(NSRect)clipRect {
        NSColor* bgColor = Nil;
    // Set the color only when its first responder and key window
    if (self == [[self window] firstResponder] && [[self window] isMainWindow] && [[self window] isKeyWindow])
    {
        bgColor = [NSColor brownColor];
    }
    else
    {
        bgColor = [NSColor windowBackgroundColor];;
    }

    NSIndexSet* selectedRowIndexes = [self selectedRowIndexes];
    if ([selectedRowIndexes containsIndex:row])
    {
        [bgColor setFill];
        NSRectFill([self rectOfRow:row]);
    }
    [super drawRow:row clipRect:clipRect]; 
}



回答4:


Add below line in your tableview method

ObjectiveC

[yourtableview setSelectionHighlightStyle:NSTableViewSelectionHighlightStyleNone];

Swift

yourtableview.selectionHighlightStyle = .none


来源:https://stackoverflow.com/questions/28461362/cocoa-osx-nstableview-change-row-highlight-color

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!