gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 17:05:19 24 4
gpt4 key购买 nike

当我关闭窗口时,我需要对 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/

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