【Swift Mac开发】自定义NSTableView选中行背景

  由于使用的是基于视图的NSTableView,因此可以将NSTableRowView子类化,将其提供给表委托方法- (NSTableRowView *)tableView:(NSTableView *)tableView rowViewForRow:(NSInteger)row;,然后在行视图类中自定义选择。

import Foundation
import Cocoa

class BMTableRowView : NSTableRowView {
    
    override func drawSelection(in dirtyRect: NSRect) {
        if self.selectionHighlightStyle != .none {
            let selectionRect = bounds.insetBy(dx: 0, dy: 0)
            NSColor.color(withRGB: 0x3578f6)!.setFill()
            let selectionPath = NSBezierPath(roundedRect: selectionRect, xRadius: 6, yRadius: 6)
            selectionPath.fill()
        }
    }
}

  使用:

    func tableView(_ tableView: NSTableView, rowViewForRow row: Int) -> NSTableRowView? {
        let rowView = BMTableRowView()
        return rowView
    }

分享链接:

工作之余,开了一个淘宝小店,分别销售日常必备生活用品,期待您的光临!点击下图,跳转店铺首页!
原文地址:https://www.cnblogs.com/xjf125/p/14769674.html