简体   繁体   English

如何更改使用NSArrayContoller作为内容源的NSTableView的选择颜色?

[英]How to change selection color for NSTableView which use NSArrayContoller as content source?

I have an NSTableView (view Based) who show info's from an NSarrayController. 我有一个NSTableView(基于视图),它显示来自NSarrayController的信息。 All connections are made from IB. 所有连接均来自IB。 Everything works fine, but blue selection color doesn't look ok with my design idea, so I want to change this color with my own color, but until now I failed. 一切正常,但是蓝色选择颜色在我的设计思路中看起来并不理想,因此我想用自己的颜色更改该颜色,但是直到现在我还是失败了。

class MyTable: NSTableView, NSTableViewDelegate {
    override func drawBackgroundInClipRect(clipRect: NSRect) {

        if self.isRowSelected( self.selectedRow ){
            //NSColor.brownColor().colorWithAlphaComponent(0.9).setFill()
           // NSBezierPath.fillRect(clipRect)
            println("selected")

        }
    }


}

My real problem is that: I have no idea where should I start, what to subclass, NSTableView , NSTableCellView etc. 我的真正问题是:我不知道应该从哪里开始,要继承什么,NSTableView,NSTableCellView等。

Recent I discovered a method to do that which say I should subclass NSTableRowView, and override drawSelectionInRect function. 最近,我发现了一种执行此操作的方法,该方法说我应该继承NSTableRowView的子类,并重写drawSelectionInRect函数。 I've did that, but in IB i don't have an NSTableRowView Object. 我已经做到了,但是在IB中,我没有NSTableRowView对象。

Thanks. 谢谢。

I did that in this way. 我是这样做的。 First subclass NSTableRowView as you said: 如您所说,第一个子类NSTableRowView:

class MyRowView: NSTableRowView {

    override func drawRect(dirtyRect: NSRect) {
        super.drawRect(dirtyRect)

        if selected == true {
            NSColor.greenColor().set()
            NSRectFill(dirtyRect)
        }
    }
}

And then just add this method: 然后只需添加此方法:

func tableView(tableView: NSTableView, rowViewForRow row: Int) -> NSTableRowView? {
    let myCustomView = MyRowView()
    return myCustomView
}

If I remember right, that should do it. 如果我没记错的话,那就应该这样做。

Edit: I just try my example and it works. 编辑:我只是尝试我的示例,它的工作原理。 In IB i set my tableview's delegate and datasource to ViewController and named my only column to "name." 在IB中,我将表视图的委托和数据源设置为ViewController,并将我的唯一列命名为“ name”。 Here is my whole ViewController class: 这是我的整个ViewController类:

class ViewController: NSViewController, NSTableViewDataSource, NSTableViewDelegate {

    var persons = [Person]()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        populate()
    }

    func populate() {
        var first = Person(name: "John")
        var second = Person(name: "Jesse Pinkman")

        persons = [first, second]
    }

    func numberOfRowsInTableView(tableView: NSTableView) -> Int {
        return persons.count
    }

    func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView? {
        if tableColumn?.identifier == "name" {
            let view = tableView.makeViewWithIdentifier(tableColumn!.identifier!, owner: self) as! NSTableCellView
            view.textField?.stringValue = persons[row].name
            return view
        }
        return nil
    }

    func tableView(tableView: NSTableView, rowViewForRow row: Int) -> NSTableRowView? {
        let myCustomView = MyRowView()
        return myCustomView
    }

}

The preferred method is to override drawSelection(in:) which the default implementation of draw(_:) calls when it needs to draw the selection for a selected row. 首选方法是重写drawSelection(in :) ,当需要绘制所选行的选择内容时, draw(_:)的默认实现会调用该方法。 You don't have to check the selection state yourself. 您不必自己检查选择状态。

override func drawSelection(in dirtyRect: NSRect) {
    NSColor.green.setFill()
    dirtyRect.fill()
}

However, if you override draw(in:) as well and do not call the superclass implementation, you will have to call drawSelection(in:) manually and determine the selection state yourself. 但是,如果您也重写了draw(in:)并且不调用超类实现,则必须手动调用drawSelection(in:)drawSelection(in:)确定选择状态。 Another benefit to overriding drawSelection(in:) is that "the selection will automatically be alpha-blended if the selection is animating in or out.". 覆盖drawSelection(in:)另一个好处是“如果选择动画或不动画,则选择将自动进行alpha混合”。

Swift 4 version of @Prontto 's excellent answer. @Prontto的Swift 4版本的绝佳答案。

class MyRowView: NSTableRowView {
    override func draw(_ dirtyRect: NSRect) {
        super.draw(dirtyRect)

        if isSelected == true {
            NSColor.green.set()
            dirtyRect.fill()
        }
    }
}

.

// In your NSTableViewDelegate
func tableView(_ tableView: NSTableView, rowViewForRow row: Int) -> NSTableRowView? {
    return MyRowView()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM