当前位置 : 主页 > 手机开发 > 其它 >

macos – 为什么NSTableCellView.backgroundStyle永远不会被设置为选定行的NSBackgroundSty

来源:互联网 收集:自由互联 发布时间:2021-06-11
在基于视图的NSTableView中,我有一个NSTableCellView的子类. 我想更改所选行的cellView的文本颜色. class CellView: NSTableCellView { override var backgroundStyle: NSBackgroundStyle { set { super.backgroundStyle = newVal
在基于视图的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?

谢谢.

虽然我仍然不知道为什么TableView / RowView没有在所选行的cellView上设置Dark背景,但我发现这是一个可接受的解决方法:

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()
        }
    }

}
网友评论