[英]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樹中即可找到它。
我敢肯定,有一種簡單,優雅,簡單的方法可以做到這一點,但我還沒發現,但是對NSTreeController
和NSOutlineView
還是NSOutlineView
的。
我在核心數據中使用以下代碼,
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
數組,直到在其中找到devName
和devID
並進行相應處理。
可能不是最優雅的解決方案,但至少最終可以奏效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.