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