在基于 View 的 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 上设置深色背景,但我发现这是一个可以接受的解决方法:
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()
}
}
}
关于macos - 为什么 NSTableCellView.backgroundStyle 永远不会为选定的行设置为 NSBackgroundStyle.Dark?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28187909/