NSTableRowView と NSTableCellView


構造

NSTableRowView と NSTableCellView は NSView-based NSTableView / NSOutlineView で使われるビューです。NSTableRowView は行全体、NSTableCellView は行列で見たときのセルにあたります。



https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/TableView/TableViewOverview/TableViewOverview.html

ハイライトカラー

行のハイライトカラーは規定ものが適用されており、システム環境設定のアピアランスで指定した強調表示色に依存しています。

行のハイライトカラーはウインドウの選択状態にも依存します。ウインドウがアクティブである時は強調表示色になりますが、インアクティブの時にはグレーになります。

ウインドウがアクティブ

ウインドウがインアクティブ

またこれらの色は NSColor でも得られます。

NSColor
// ウインドウがアクティブで、行が選択されている時の色
alternateSelectedControlColor()
// ウインドウがインアクティブで、行が選択されている時の色
secondarySelectedControlColor()

ハイライトカラーの変更

Cocoa Touch (iOS) では UITableViewCell の backgroundView だとかを使って色を変更しますが、Cocoa ではちょっと面倒くさいです。NSTableRowView のサブクラスで drawSelectionInRect(dirtyRect:) をオーバーライドしてベジェパスで色を塗りつぶします。

MyTableRowView.swift
import Cocoa

class MyTableRowView: NSTableRowView {
    override func drawSelectionInRect(dirtyRect: NSRect) {
        // ここではハイライトスタイルを Regular に限定している
        if selectionHighlightStyle == .Regular {
            if selected == true {
                if emphasized == true {
                    // ウインドウがアクティブ状態
                    NSColor(red: 0.188, green: 0.514, blue: 0.985, alpha: 1.0).set()
                    var fillRect = bounds
                    fillRect.size.height -= 1
                    NSBezierPath.fillRect(fillRect)
                    return
                }
                //else {
                    // ウインドウがインアクティブの状態
                //}
            }
        }

        super.drawSelectionInRect(dirtyRect)
    }
}
NSOutlineViewDelegate
extension ViewController: NSOutlineViewDelegate {
func outlineView(outlineView: NSOutlineView, rowViewForItem item: AnyObject) -> NSTableRowView? {
        let identifier = "Row"
        var rowView = outlineView.makeViewWithIdentifier(identifier, owner: self) as? MyTableRowView
        if rowView == nil {
            rowView = MyTableRowView(frame: NSZeroRect)
            rowView!.identifier = identifier
        }

        return rowView
    }
}

ハイライトカラーを青色にしてみた例。