【发布时间】:2016-04-13 19:47:29
【问题描述】:
我有一个NSOutlineView,为此我写了datasource 和delegate。当我将一个项目添加到一个组并且该组项目处于折叠状态时,parentForItem 返回nil。
这是我用来测试的代码。
- (IBAction)addItemToGroup:(id)sender {
TLItem *theNewItem= [[TLItem alloc] initWithTitle:@"My New Item"];
TLItem *theGroupItem = [self.sourceListItems objectAtIndex:0];
NSMutableArray *theItemList = [theGroupItem children];
[theItemList addObject:theNewItem];
[self.sourceListOutlineView reloadData];
TLItem *newItemParent = [self.sourceListOutlineView parentForItem:theNewItem];
NSLog(@"newItemParent = %@", newItemParent);
}
如果 theGroupItem 已展开,此方法会记录以下内容:
newItemParent = TLItem: 0x60800004bbb0
如果 theGroupItem 已折叠,此方法会记录以下内容:
newItemParent = (null)
如何获取新添加项目的父项?
注意:我意识到这是一个愚蠢的例子,但在我的实际代码中,我需要能够向上查找层次结构中的所有父级。
【问题讨论】:
标签: objective-c macos cocoa nsoutlineview