繁体   English   中英

在NSOutlineView选择上获取NSTreeController数据

[英]Getting NSTreeController Data on NSOutlineView selection

我建立了一个NSOutlineView ,它从NSTreeController获取动态更新的数据,并且一切正常。 我似乎无法做的是根据NSOutlineView中的用户选择从那里向后工作。

    var deviceStore = [TreeNode]()

是我的支持数据存储库,它是实时更新的,它是Device对象数组,可以(也可以不)包含Service对象数组作为子对象。

所有这一切。 但是,当我在“大纲视图”中选择一行时,我需要返回到deviceStore中的原始对象,或者至少从“大纲视图”中获取显示的数据,以便我可以在deviceStore中查找原始项目。

我得到的是func outlineViewSelectionDidChange(_ notification: Notification) {} ,当做出选择时会调用它,我可以通过treeController.selectedNodes从中提取NSTreeController TreeNode,但是从那里,我就在杂草中。 selectedNodes是所选节点的完整数组,因此,如果它是子(叶)节点,则它包括其父节点及其所有同级节点。

给然后表显示在这里: 轮廓视图

selectedNodes数组如下所示:

<NSTreeControllerTreeNode: 0x6080000c4590>, child nodes {
    0:<NSTreeControllerTreeNode: 0x6000000ca6b0>, child nodes {
        0:<NSTreeControllerTreeNode: 0x6000000caf70>, child nodes {}
        1:<NSTreeControllerTreeNode: 0x6000000cafe0>, child nodes {}
        2:<NSTreeControllerTreeNode: 0x6000000cb050>, child nodes {}
    }
    1:<NSTreeControllerTreeNode: 0x6080000d1790>, child nodes {
        0:<NSTreeControllerTreeNode: 0x6000000cce80>, child nodes {}
    }
}

并且selectedIndex为4。

我看不到如何从该信息返回到我的数据模型中的deviceStore [0] .serviceStore [2]。

如果我可以从选定的行中检索“服务ID”列中的值,则只需走到deviceStore树中即可找到它。

我敢肯定,有一种简单,优雅,简单的方法可以做到这一点,但我还没发现,但是对NSTreeControllerNSOutlineView还是NSOutlineView的。

您可以尝试直接访问关联的对象,如下所示:

let selectedService = treeController.selectedObjects.first as? Service

文档在这里

还要确保将NSTreeController正确配置为使用类的对象:

在此处输入图片说明

或者(如果您想直接使用数据源),则可能需要获取 NSTreeController所选对象的索引路径

var selectionIndexPath: IndexPath? { get }

我在核心数据中使用以下代码,

func outlineViewSelectionDidChange(_ notification: Notification) {
    let item = outlineView.item(atRow: outlineView.selectedRow) as! NSTreeNode
    let fileItem = item.representedObject as! FileItemMO

    // Do what you need to do with object fileItem

}

我从来没有真正回到过以用户在TreeView中单击的位置为基础的TreeController后备存储数据。 能够做的是,虽然向后工作数据。

func outlineViewSelectionDidChange(_ notification: Notification) {
    let selectedIndex = (notification.object as AnyObject).selectedRow!
    let selCol1 = outlineView.view(atColumn: 0, row: selectedIndex, makeIfNecessary: false)?.subviews.last as! NSTextField
    let selCol2 = outlineView.view(atColumn: 1, row: selectedIndex, makeIfNecessary: false)?.subviews.last as! NSTextField

    let devName = selCol1.stringValue
    let devID = selCol2.stringValue
...
}

然后,我可以“遍历” deviceStrore数组,直到在其中找到devNamedevID并进行相应处理。

可能不是最优雅的解决方案,但至少最终可以奏效。

暂无
暂无

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

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