2012-04-03 46 views
1

我需要在关闭窗口时对我的NSOutlineView中的所有对象执行操作。如何遍历我的NSOutlineView的所有项目?

(父母和孩子,以及孩子的孩子)。 无论项目是否展开都没关系,我只需要在大纲视图中的所有项目上执行选择器。

感谢

回答

2

假设你正在使用NSOutlineViewDataSource和不绑定的,你可以做这样的:

- (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贩卖的所有对象的完整遍历。

仅供参考:如果您使用可可绑定,则不会按原样工作。但是,如果是这样的话,你可以对你绑定的NSTreeController(或其他任何东西)使用类似的方法(即代理栈遍历)。

HTH

+0

由于某种原因,这不适用于我。这给了我堆栈上的0个对象。我没有使用绑定。 – SpaceDog 2015-03-16 16:32:34

+0

这里的代码并不打算把所有的项目放到'stack'中,它只是用来访问每个项目。 'stack'在开始和结束时将是空的。 – ipmcc 2015-03-16 16:58:23

+0

我发现必须选择一个项目才能使用此方法。因为这个想法是遍历所有项目,我从选择项目0开始,然后运行你的方法。谢谢 – SpaceDog 2015-03-16 18:12:31