在我的一个项目中使用它之前,我创建了一个小型测试项目来使用 NSOutlineView。我使用内容绑定到数组的 NSTreeController 成功地显示了带有子项的项目列表。
现在,当我创建这个对象时,我花了很长时间才意识到我的数组内容只有在我的 init 方法中创建它们时才会显示: - (id)init { self = [super init];
if (self) {
results = [NSMutableArray new];
NSMutableArray *collection = [[NSMutableArray alloc] init];
// Insert code here to initialize your application
NSMutableDictionary *aDict = [[NSMutableDictionary alloc] init];
[aDict setValue:@"Activities" forKey:@"name"];
NSMutableArray *anArray = [NSMutableArray new];
for (int i; i<=3 ; i++) {
NSMutableDictionary *dict = [NSMutableDictionary new];
[dict setValue:[NSString stringWithFormat:@"Activity %d", i] forKeyPath:@"name"];
[anArray addObject:dict];
}
results = collection;
}
return self;
}
如果我将相同的代码放在 applicationDidFinishLaunching 中,它就不会显示这些项目。
尝试将项目添加到视图时,我现在面临同样的问题。我对使用 NSTreeController 的理解是,它处理的内容类似于 NSArrayController 对 NSTableView 所做的事情(OutlineView 是一个子类和全部)。但是,每当我使用符合 KV 的方法将项目添加到数组时,这些项目都不会显示在我的视图中。
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSMutableDictionary *cDict = [[NSMutableDictionary alloc] init];
[cDict setValue:@"Calls" forKey:@"name"];
[results addObject:cDict];
[outlineView reloadData];
}
添加对象后,我还尝试在大纲视图上调用 reloadData,但这似乎没有被调用。我错过了什么?
这是我的项目的链接:https ://dl.dropboxusercontent.com/u/5057512/Outline.zip