Swift での the_critic のソリューション: (XCode 7 beta3、Swift 2.0 でテスト済み)
class CustomTableRowView: NSTableRowView {
override var selected: Bool {
willSet(newValue) {
super.selected = newValue;
needsDisplay = true
}
}
override func drawBackgroundInRect(dirtyRect: NSRect) {
let context: CGContextRef = NSGraphicsContext.currentContext()!.CGContext
if !self.selected {
CGContextSetFillColorWithColor(context, NSColor.clearColor().CGColor)
} else {
CGContextSetFillColorWithColor(context, NSColor.redColor().CGColor)
}
CGContextFillRect(context, dirtyRect)
}
}
ビューコントローラー:
class ViewController: NSViewController, NSTableViewDelegate, NSTableViewDataSource {
// [...]
func tableView(tableView: NSTableView, rowViewForRow row: Int) -> NSTableRowView? {
guard let rowView = tableView.makeViewWithIdentifier("RowView", owner: nil) as! CustomTableRowView? else {
let rowView = CustomTableRowView()
rowView.identifier = "RowView"
return rowView
}
return rowView
}
}