如何更改使用 NSArrayContoller 作为内容源的 NSTableView 的选择颜色? [英] How to change selection color for NSTableView which use NSArrayContoller as content source?

查看:46
本文介绍了如何更改使用 NSArrayContoller 作为内容源的 NSTableView 的选择颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 NSTableView(基于视图),它显示来自 NSarrayController 的信息.所有连接均由 IB 进行.一切正常,但蓝色选择颜色与我的设计理念不符,所以我想用我自己的颜色更改此颜色,但直到现在我都失败了.

I have an NSTableView (view Based) who show info's from an NSarrayController. All connections are made from 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")

        }
    }


}

我真正的问题是:我不知道我应该从哪里开始,子类化什么, NSTableView , NSTableCellView 等等.

My real problem is that: I have no idea where should I start, what to subclass, NSTableView , NSTableCellView etc.

最近我发现了一种方法,它说我应该继承 NSTableRowView,并覆盖 drawSelectionInRect 函数.我已经这样做了,但是在 IB 中我没有 NSTableRowView 对象.

Recent I discovered a method to do that which say I should subclass NSTableRowView, and override drawSelectionInRect function. I've did that, but in IB i don't have an NSTableRowView Object.

谢谢.

推荐答案

我就是这样做的.正如你所说的第一个子类 NSTableRowView :

I did that in this way. First subclass NSTableRowView as you said:

class MyRowView: NSTableRowView {

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

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

然后只需添加此方法:

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

如果我没记错的话,应该可以.

If I remember right, that should do it.

我只是尝试我的例子,它有效.在 IB 中,我将 tableview 的委托和数据源设置为 ViewController,并将我唯一的列命名为name".这是我的整个 ViewController 类:

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." Here is my whole ViewController class:

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
    }

}

这篇关于如何更改使用 NSArrayContoller 作为内容源的 NSTableView 的选择颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆