objective-c - 如何迭代 NSOutlineView 的所有项目?

标签 objective-c cocoa

当我关闭窗口时,我需要对 NSOutlineView 中的所有对象执行操作。

( parent 和 child ,以及 child 的 child )。 项目是否展开并不重要,我只需要对大纲 View 中的所有项目执行选择器即可。

谢谢

最佳答案

假设您使用的是 NSOutlineViewDataSource 而不是绑定(bind),您可以这样做:

- (void)iterateItemsInOutlineView: (NSOutlineView*)outlineView
{
    id<NSOutlineViewDataSource> dataSource = outlineView.dataSource;
    NSMutableArray* stack = [NSMutableArray array];
    do
    {
        // Pop an item off the stack
        id currentItem = stack.lastObject;
        if (stack.count) 
            [stack removeLastObject];

        // Push the children onto the stack
        const NSUInteger childCount = [dataSource outlineView: outlineView numberOfChildrenOfItem: currentItem];
        for (NSUInteger i = 0; i < childCount; ++i) 
            [stack addObject: [dataSource  outlineView: outlineView child: i ofItem: currentItem]];

        // Visit the current item.
        if (nil != currentItem)
        {
            // Do whatever you want to do to each item here...
        }        
    } while (stack.count);
}

这应该实现对 NSOutlineViewDataSource 提供的所有对象的完整遍历。

仅供引用:如果您使用 cocoa 绑定(bind),则这将无法按原样工作。不过,如果是这种情况,您可以对您绑定(bind)到的 NSTreeController (或其他任何东西)使用类似的方法(即代理堆栈遍历)。

HTH

关于objective-c - 如何迭代 NSOutlineView 的所有项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9991822/

相关文章:

cocoa - 在谓词中使用@min

objective-c - 在没有新初始化的情况下使用类中的数据

objective-c - 在模拟器中播放 - objective-c

iphone - XCode (iOS) 中委托(delegate)的自动导入回调

objective-c - 计算数字数组的可能排列

swift - 如何更新 NSView 图层位置和 anchor 以与鼠标事件的 NSView 框架同时进行转换

ios - UIColorresolveClassMethod 未从选择器解析

objective-c - XCode 文档 - 离线阅读

objective-c - 我可以在 xcode 中做些什么来解决这不是 utf-8 静态库问题吗?

macos - Apple SidebarDemo 的奇怪行为