user1007895
user1007895

Reputation: 3965

Background Color for row in NSOutlineView

How do I set the background color for 1 particular row in an NSOutlineView? I have tried this with no luck:

func outlineView(_ outlineView: NSOutlineView, rowViewForItem item: Any) -> NSTableRowView? {
    let newRow = NSTableRowView()
    newRow.backgroundColor = NSColor.red
    return newRow
}

Upvotes: 3

Views: 871

Answers (1)

hashemi
hashemi

Reputation: 2678

What you need instead is outlineView(didAdd:forRow). Here's what your code would look like:

func outlineView(_ outlineView: NSOutlineView, didAdd rowView: NSTableRowView, forRow row: Int) {
    rowView.backgroundColor = .red
}

Note that unlike most other outlineView delegate methods, outlineView(didAdd:forRow) gives you a row index instead of an item. You can get the item by calling outlineView.item(atRow: row).

The reason your code didn't work is that outlineView(rowViewForItem:) creates the NSTableRowView before any styling is applied. The styling you were applynig there was being overwritten outlineView applying its own styling. The method is meant to be used for subclassing NSTableRowView.

Upvotes: 5

Related Questions