NSOutlineView,如何获取选定的单元格 [英] NSOutlineView, how to get the selected cell

查看:214
本文介绍了NSOutlineView,如何获取选定的单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从NSOutlineView控件中获取选定的单元格.我在这里找到了解决方案:如何获取所选内容来自NSOutlineView的单元格?.它说

I want to get the selected cell from the NSOutlineView control. I found a solution here: How can I get the selected cell from a NSOutlineView?. It says

使用委托.当单元格更改其选择状态时,将调用willDisplayCell:.

Use the delegate. willDisplayCell: is called when a cell changes its selection state.

但是,当我对其进行测试时,我发现我的willDisplayCell:没有被调用.

However when I test it, I found that my willDisplayCell: is not be called.

这是我的代码,可以正常运行,但是从未调用过willDisplayCell:方法.我在哪里弄错了?谢谢.

Here's my code, it can be run normally, but the willDisplayCell: method has never been called. Where did I make a mistake? Thanks.

class TreeNode: NSObject{
    var name: String = ""
    private(set) var isLeaf: Bool = false
    var children: [TreeNode]?

    init(name: String, isLeaf: Bool){
        self.name = name
        self.isLeaf = isLeaf
        if !isLeaf{
            children = [TreeNode]()
        }
    }
}

class ViewController: NSViewController {

    @IBOutlet weak var sourceList: NSOutlineView!

    private var data = TreeNode(name: "Root", isLeaf: false)

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

        for i in 0..<10{
            let node = TreeNode(name: "name \(i)", isLeaf: i % 2 == 0)
            data.children?.append(node)
        }
    }
}

extension ViewController: NSOutlineViewDataSource {
    func outlineView(_ outlineView: NSOutlineView, numberOfChildrenOfItem item: Any?) -> Int {
        if let item = item as? TreeNode, !item.isLeaf {
            return item.children!.count
        }
        return 1
    }
    func outlineView(_ outlineView: NSOutlineView, isItemExpandable item: Any) -> Bool {
        return !((item as? TreeNode)?.isLeaf ?? false)
    }
    func outlineView(_ outlineView: NSOutlineView, child index: Int, ofItem item: Any?) -> Any {
        if let item = item as? TreeNode {
            if item.isLeaf{
                return item
            }else{
                return item.children![index]
            }
        }
        return data
    }
}
extension ViewController: NSOutlineViewDelegate {

    func outlineView(_ outlineView: NSOutlineView, willDisplayCell cell: Any, for tableColumn: NSTableColumn?, item: Any) {
        print("called")
    }

    func outlineView(_ outlineView: NSOutlineView, viewFor tableColumn: NSTableColumn?, item: Any) -> NSView? {
        let cell: NSTableCellView?
        if let item = item as? TreeNode{
            cell = outlineView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue:  "DataCell"), owner: self) as? NSTableCellView
            cell?.textField?.stringValue = item.name
        }else{
            cell = nil
        }
        return cell
    }
}

推荐答案

我自己已经解决了这个问题.以下代码是如何获取所选单元格的信息:

I've solved this problem myself. The following code is how to get the selected cell:

func getSelectedCell() -> NSTableCellView? {
    if let view = self.sourceList.rowView(atRow: self.sourceList.selectedRow, makeIfNecessary: false) {
        return view.view(atColumn: self.sourceList.selectedColumn) as? NSTableCellView
    }
    return nil
}

现在我可以通过代码getSelectedCell()?.textField访问NSTextField控件.

Now I can access the NSTextField control by the code getSelectedCell()?.textField.

这篇关于NSOutlineView,如何获取选定的单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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