如何清晰地获得NSTreeController或NSOutlineView中对象的NSIndexPath?

4
在查阅了文档后,我没有找到一种相对简单的方法来获取绑定在NSOutlineViewNSTreeController上的对象的索引路径。因此,每当需要执行需要使用它们的操作(例如从树中删除特定项目)时,我都不得不编写非常丑陋的代码来手动组装索引路径。
有没有比[[NSIndexPath indexPathWithIndex:<blah>] indexPathByAddingIndex: <blah>]更好的方法呢?

链接的答案仅适用于 NSTreeController,那么对于 NSOutlineView 或 dataSource 的解决方案是什么? - silicontrip
1个回答

2
您可以轻松地构建路径,如下所示:
NSUInteger indexes[4] = {2, 3, 1, 0};
NSIndexPath* path = [NSIndexPath indexPathWithIndexes:indexes length:4];

要获取特定模型对象的索引路径,请参考我的回答

2
[_treeController.arrangedObjects descendantNodeAtIndexPath:[NSIndexPath indexPathWithIndex:[someOutline selectedRow]]] 必须是我见过的 API 中最糟糕的构造之一。苹果公司应该为此感到羞耻 - Alex Gray
1
正确无误。总的来说,NSTreeController 不是苹果最好的时刻。 - Rob Keniger
1
等等,更糟糕的是...仅仅为了获取“树控制”情况下高亮节点的“值”,需要使用以下代码:[[[_treeController.arrangedObjects descendantNodeAtIndexPath:[NSIndexPath indexPathWithIndex:[sender selectedRow]]] representedObject] representedObject]。哈哈,这个双重的representedObject让我发疯!就像他们吸食了大麻一样...“嘿,让我们称它为objectValue”,“不,我说是representedObject,哇!”...“那selectionIndexes怎么样?”,“不,是selectedIndexPath”,“不,是NSOutlineViewSelectedPathIndexesPathBindingOption!这听起来很棒。”哈! - Alex Gray

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接