Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change selection color for NSTableView which use NSArrayContoller as content source?

I have an NSTableView (view Based) who show info's from an NSarrayController. All connections are made from IB. Everything works fine, but blue selection color doesn't look ok with my design idea, so I want to change this color with my own color, but until now I failed.

class MyTable: NSTableView, NSTableViewDelegate {
    override func drawBackgroundInClipRect(clipRect: NSRect) {

        if self.isRowSelected( self.selectedRow ){
            //NSColor.brownColor().colorWithAlphaComponent(0.9).setFill()
           // NSBezierPath.fillRect(clipRect)
            println("selected")

        }
    }


}

My real problem is that: I have no idea where should I start, what to subclass, NSTableView , NSTableCellView etc.

Recent I discovered a method to do that which say I should subclass NSTableRowView, and override drawSelectionInRect function. I've did that, but in IB i don't have an NSTableRowView Object.

Thanks.

like image 315
C-Viorel Avatar asked Apr 06 '15 15:04

C-Viorel


1 Answers

Swift 4 version of @Prontto 's excellent answer.

class MyRowView: NSTableRowView {
    override func draw(_ dirtyRect: NSRect) {
        super.draw(dirtyRect)

        if isSelected == true {
            NSColor.green.set()
            dirtyRect.fill()
        }
    }
}

.

// In your NSTableViewDelegate
func tableView(_ tableView: NSTableView, rowViewForRow row: Int) -> NSTableRowView? {
    return MyRowView()
}
like image 60
Simon Epskamp Avatar answered Nov 07 '22 19:11

Simon Epskamp