Cocoa osx NSTableview change row highlight color
Asked Answered
W

4

6

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://mcmap.net/q/369861/-change-selection-color-on-view-based-nstableview

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.

Williford answered 11/2, 2015 at 18:10 Comment(1)
Are you using view based tableView or cell based.Isia
W
8

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;
}
Williford answered 12/2, 2015 at 10:59 Comment(0)
R
7

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()
}
Ricketts answered 30/9, 2016 at 15:13 Comment(1)
This works pretty well, but the trouble I'm having is that the row highlight stays that color even if the table is in its "blurred" state. For example, if you select a row, then click into an NSTextField, the table row is still highlighted but the text goes dark again. Here's an example video: d.pr/v/0KmToP ...any idea how to change that state's row background color?Yeargain
I
2

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]; 
}
Isia answered 11/2, 2015 at 22:31 Comment(0)
T
0

Add below line in your tableview method

ObjectiveC

[yourtableview setSelectionHighlightStyle:NSTableViewSelectionHighlightStyleNone];

Swift

yourtableview.selectionHighlightStyle = .none
Tympanitis answered 8/9, 2017 at 7:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.