In a view-based NSTableView, I have a subclass of NSTableCellView.
I want to change text color for the selected row's cellView.
class CellView: NSTableCellView {
    override var backgroundStyle: NSBackgroundStyle {
        set {
            super.backgroundStyle = newValue
            self.udpateSelectionHighlight()
        }
        get {
            return super.backgroundStyle;
        }
    }
    func udpateSelectionHighlight() {
        if ( self.backgroundStyle == NSBackgroundStyle.Dark ) {
            self.textField?.textColor = NSColor.whiteColor()
        } else if( self.backgroundStyle == NSBackgroundStyle.Light ) {
            self.textField?.textColor = NSColor.blackColor()
        }
    }
}
The problem is that all cellViews are set with NSBackgroundStyle.Light.
My selection is custom-drawn in a subclass of NSTableRowView.
class RowView: NSTableRowView {
    override func drawSelectionInRect(dirtyRect: NSRect) {
        if ( self.selectionHighlightStyle != NSTableViewSelectionHighlightStyle.None ) {
            var selectionRect = NSInsetRect(self.bounds, 0, 2.5)
            NSColor( fromHexString: "d1d1d1" ).setFill()
            var selectionPath = NSBezierPath(
                roundedRect: selectionRect,
                xRadius: 10,
                yRadius: 60
            )
            // ...
            selectionPath.fill()
        }
    }
    // ...
}
Why isn't the selected row cellView's backgroundStyle property set to Dark?
Thanks.
While I still don't know why doesn't the TableView/RowView set the Dark background on the selected row's cellView, I found this to be an acceptable workaround:
class CellView: NSTableCellView {
    override var backgroundStyle: NSBackgroundStyle {
        set {
            if let rowView = self.superview as? NSTableRowView {
                super.backgroundStyle = rowView.selected ? NSBackgroundStyle.Dark : NSBackgroundStyle.Light
            } else {
                super.backgroundStyle = newValue
            }
            self.udpateSelectionHighlight()
        }
        get {
            return super.backgroundStyle;
        }
    }
    func udpateSelectionHighlight() {
        if ( self.backgroundStyle == NSBackgroundStyle.Dark ) {
            self.textField?.textColor = NSColor.whiteColor()
        } else {
            self.textField?.textColor = NSColor.blackColor()
        }
    }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With