NSTableView 中每一行是用NSTableRowView表示的,因此可以继承此类,重写点击时的drawSelection方法
示例更改了点击时的行颜色,并且在最左边画了一条线:
class CSTableRowView: NSTableRowView {
override func drawSelection(in dirtyRect: NSRect) {
if self.selectionHighlightStyle != .none {
// 填充背景颜色
let fillColor = NSColor(red: 35.0/255, green: 63.0/255, blue: 95.0/255, alpha: 1)
fillColor.set()
dirtyRect.fill()
// 绘制左侧边框
let lineColor = NSColor(red: 0.20, green: 0.85, blue: 0.99, alpha: 1.00)
lineColor.setFill()
let lineRect = NSRect(x: 0, y: 0, width: 2, height: dirtyRect.height)
lineRect.fill()
}
}
}
在代理方法中,使用自定义的 rowView
// 示例用的 outlineView
func outlineView(_ outlineView: NSOutlineView, rowViewForItem item: Any) -> NSTableRowView? {
var rowView = outlineView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "rowView"), owner: self) as? CSTableRowView
if rowView == nil {
rowView = CSTableRowView()
rowView?.identifier = NSUserInterfaceItemIdentifier(rawValue: "rowView")
}
return rowView
}