覆盖NSTreeController的arrangeObjects

时间:2017-03-21 06:33:07

标签: swift macos cocoa cocoa-bindings nstreecontroller

在我的项目中,NSTreeController绑定到NSOutlineView(可可绑定)。出于某种原因我无法使用NSPredicate 来过滤Core Data中的数据。所以我试图覆盖arrangedObjects的{​​{1}},但最重要的NSTreeController 甚至没有被解雇。 (我在其中设置了一个调试断点。)代码编译得很好,我已经将实例更改为arrangedObjects。我只在邮件列表中找到an unanswered question asked a decade ago

CleanTreeController

我找到了

  

NSTreeController的arrangeObjects方法返回的值是   不透明。你应该只观察这个属性来确定a   发生了变化。

doc中的

。那么// Subclass of NSTreeController class CleanTreeController: NSTreeController { init() { // This gets called. super.init(content: nil) } required init?(coder: NSCoder) { super.init(coder: coder) } override var arrangedObjects: Any { let oldArranged = super.arrangedObjects let selector = NSSelectorFromString("childNodes") guard let oldChildren = (oldArranged as! NSObject) .perform(selector).takeUnretainedValue() as? [NSTreeNode] else { return oldArranged } let children = oldChildren.filter { (treeNode) in //... do some filtering } return TreeNodeProxy(with: children) } } // Homemade NSTreeNodeProxy class TreeNodeProxy : NSObject { fileprivate var rootNode : NSTreeNode init(with children : [NSTreeNode]) { self.rootNode = NSTreeNode() self.rootNode.mutableChildren.addObjects(from: children) } @objc(childNodes) var children: [NSTreeNode]? { return rootNode.children } @objc(descendantNodeAtIndexPath:) func descendant(at indexPath: IndexPath) -> NSTreeNode? { return rootNode.descendant(at: indexPath) } } 如何在不阅读({直接)的情况下消费NSOutineView

1 个答案:

答案 0 :(得分:0)

在xib或代码中设置fetchPredicate的{​​{1}}。