1

Swift 4, macOS 10.13

I have an NSOutlineView and I'm trying to customize the look of each row when the user clicks it.

I have a view-based NSTableCellView subclass that I'm using for the cell. When I override backgroundStyle, the icon change works perfectly. But the text color is doing something weird.

Here's a video to demonstrate: http://d.pr/v/suTD8B

Here's my code:

class MenuItemCell: NSTableCellView{
  @IBOutlet weak var label: NSTextField!
  @IBOutlet weak var icon: NSImageView!

  //Customizes the selected state of menu row
  override var backgroundStyle: NSView.BackgroundStyle {
    set{
      if let rowView = self.superview as? NSTableRowView {
        super.backgroundStyle = rowView.isSelected ? .dark : .light
      }
      if self.backgroundStyle == .dark {
        label.textColor = NSColor.white
        icon.image = NSImage(named: NSImage.Name(rawValue: "menu-project-selected"))
      }else{
        label.textColor = NSColor.menuTextColor
        icon.image = NSImage(named: NSImage.Name(rawValue: "menu-project"))
      }
    }
    get{ return super.backgroundStyle }
  }
}

Any idea what's wrong? I've been searching for hours and can't figure it out.

Clifton Labrum
  • 13,053
  • 9
  • 65
  • 128

1 Answers1

0

It turns out that this would never work reliably while the NSOutlineView Highlight was set to Source List in the storyboard editor. It was really unreliable.

Once I changed it to Regular then it worked fine.

enter image description here enter image description here

Clifton Labrum
  • 13,053
  • 9
  • 65
  • 128