gpt4 book ai didi

objective-c - NS 大纲 View : Expand all objects of a specific class only

转载 作者:太空狗 更新时间:2023-10-30 03:53:54 31 4
gpt4 key购买 nike

NSOutlineView 中扩展特定类的所有对象的最有效方法是什么? (我的大纲 View 的数据源是一个 NSTreeController)。

假设我有

classA
classA
- classA
- classC
- classC
- classB
- classC
- classC
classB
- classC

我只想扩展 classA 对象。我是否需要遍历整个树来检查每个对象属于哪个类?

更新对不起,我必须更正。 outlineView 对象是来自 NSTreeController 数据源的 NSTreeNodes。只有“representedObject”是我的自定义类。

所以这些类的结构是正确的,但它们不能作为大纲 View 的节点直接访问。

最佳答案

就其值(value)而言,我在评论中提到的重要“胶水”或多或少直接来自这篇精彩的博文:Saving the expand state of a NSOutlineView .

我把树中的所有类都取出来,并使它们成为带有 expanded 的抽象类的子类。属性(property),所以每个representedObjectNSTreeNodeexpanded作为属性(property)。

但如果您不关心坚持 expanded,您甚至可能不需要这样做在您的数据模型中。

最直接的做法是迭代行:

    for (NSUInteger row = 0; row < [outlineView numberOfRows]; row++)
{
// Expand item if it's an classA
NSTreeNode* treeNode = [outlineView itemAtRow:row];
if ([treeNode.representedObject isKindOfClass:[ClassA class]])
[sender.animator expandItem:treeNode];
}

...您会注意到 for 循环从引用的博客文章中借用了大量结构。

所以我想我的回答是懒惰的,“是的,迭代整棵树。”至少显示的树。

编辑: 对于那些对 MVC 有点狂热的人,我现在觉得有必要指定上面的代码应该在您使用的类中作为 NSOutlineView Controller ,通常会实现 <NSOutlineViewDelegate>

关于objective-c - NS 大纲 View : Expand all objects of a specific class only,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31319196/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com