5

ビューベースの NSTableView には、NSTableCellView のサブクラスがあります。

選択した行の cellView のテキストの色を変更したい。

class CellView: NSTableCellView {

    override var backgroundStyle: NSBackgroundStyle {
        set {
            super.backgroundStyle = newValue

            self.udpateSelectionHighlight()
        }
        get {
            return super.backgroundStyle;
        }
    }

    func udpateSelectionHighlight() {
        if ( self.backgroundStyle == NSBackgroundStyle.Dark ) {
            self.textField?.textColor = NSColor.whiteColor()
        } else if( self.backgroundStyle == NSBackgroundStyle.Light ) {
            self.textField?.textColor = NSColor.blackColor()
        }
    }

}

問題は、すべての cellViews が NSBackgroundStyle.Light で設定されていることです。

私の選択は、NSTableRowView のサブクラスでカスタム描画されます。

class RowView: NSTableRowView {

    override func drawSelectionInRect(dirtyRect: NSRect) {
        if ( self.selectionHighlightStyle != NSTableViewSelectionHighlightStyle.None ) {

            var selectionRect = NSInsetRect(self.bounds, 0, 2.5)
            NSColor( fromHexString: "d1d1d1" ).setFill()
            var selectionPath = NSBezierPath(
                roundedRect: selectionRect,
                xRadius: 10,
                yRadius: 60
            )
            // ...
            selectionPath.fill()
        }
    }

    // ...

}

選択した行の cellView の backgroundStyle プロパティが Dark に設定されていないのはなぜですか?

ありがとう。

4

1 に答える 1

6

TableView/RowView が選択した行の cellView に暗い背景を設定しない理由はまだわかりませんが、これは許容できる回避策であることがわかりました。

class CellView: NSTableCellView {

    override var backgroundStyle: NSBackgroundStyle {
        set {
            if let rowView = self.superview as? NSTableRowView {
                super.backgroundStyle = rowView.selected ? NSBackgroundStyle.Dark : NSBackgroundStyle.Light
            } else {
                super.backgroundStyle = newValue
            }
            self.udpateSelectionHighlight()
        }
        get {
            return super.backgroundStyle;
        }
    }

    func udpateSelectionHighlight() {
        if ( self.backgroundStyle == NSBackgroundStyle.Dark ) {
            self.textField?.textColor = NSColor.whiteColor()
        } else {
            self.textField?.textColor = NSColor.blackColor()
        }
    }

}
于 2015-01-28T09:49:51.010 に答える