我正在尝试了解如何使用NSTreeController。当我添加一个方法(下面提供了SourceView示例的类和方法的段)来迭代目录的内容,该目录通过NSTreeController传输到NSOutlineView。但是,NSOutlineView只显示第一个对象(根对象)(您可以在下面的方案中看到它)。
类NSTreeController方法:
- (void)performAddChild:(TreeAdditionObj *)treeAddition {
if ([[treeController selectedObjects] count] > 0) {
// we have a selection
if ([[[treeController selectedObjects] objectAtIndex:0] isLeaf]) {
// trying to add a child to a selected leaf node, so select its parent for add
[self selectParentFromSelection];
}
}
// find the selection to insert our node
NSIndexPath *indexPath;
if ([[treeController selectedObjects] count] > 0) {
// we have a selection, insert at the end of the selection
indexPath = [treeController selectionIndexPath];
indexPath = [indexPath indexPathByAddingIndex:[[[[treeController selectedObjects] objectAtIndex:0] children] count]];
} else {
// no selection, just add the child to the end of the tree
indexPath = [NSIndexPath indexPathWithIndex:[contents count]];
}
// create a leaf node
BaseNode *node = [[BaseNode alloc] initLeaf];
node.urlString = [treeAddition nodeURL];
if ([treeAddition nodeURL]) {
if ([[treeAddition nodeURL] length] > 0) {
// the child to insert has a valid URL, use its display name as the node title
if ([treeAddition nodeName])
node.nodeTitle = [treeAddition nodeName];
else
node.nodeTitle = [[NSFileManager defaultManager] displayNameAtPath:[node urlString]];
}
}
// the user is adding a child node, tell the controller directly
[treeController insertObject:node atArrangedObjectIndexPath:indexPath];
// adding a child automatically becomes selected by NSOutlineView, so keep its parent selected
if ([treeAddition selectItsParent])
[self selectParentFromSelection];
}
- (void)addChild:(NSString *)url withName:(NSString *)nameStr selectParent:(BOOL)select {
TreeAdditionObj *treeObjInfo = [[TreeAdditionObj alloc] initWithURL:url
withName:nameStr
selectItsParent:select];
if (buildingOutlineView) {
// add the child node to the tree controller, but on the main thread to avoid lock ups
[self performSelectorOnMainThread:@selector(performAddChild:)
withObject:treeObjInfo
waitUntilDone:YES];
} else {
[self performAddChild:treeObjInfo];
}
}
显示目录的枚举对象的方法:
- (void)addFinderSection {
[self addFolder:@"FINDER FILES"];
NSError *error = nil;
NSEnumerator *urls = [[[NSFileManager defaultManager] contentsOfDirectoryAtURL:self.url includingPropertiesForKeys:[NSArray arrayWithObjects: nil] options:(NSDirectoryEnumerationSkipsHiddenFiles) error:&error] objectEnumerator];
for (NSURL *url in urls) {
BOOL isDirectory;
if ([[NSFileManager defaultManager] fileExistsAtPath:[url path] isDirectory:&isDirectory]) {
if (isDirectory) {
if (![[NSWorkspace sharedWorkspace] isFilePackageAtPath:[url path]]) {
NSLog(@"IS DIRECTORY %@", url);
[self addChild:[url path] withName:NO selectParent:YES];
}
} else {
NSLog(@"IS FIlE %@", url);
[self addChild:[url path] withName:NO selectParent:YES];
}
}
}
[self selectParentFromSelection];
}
我的错误可能就是NSTreeController没有区分简单对象(文件)和目录(文件夹)。
但是,当我在没有NSTreeController的情况下填写NSOutlineView时,所有内容都显示正确:
注意:如果使用方法addFolder :(在SourceView示例中,它用于创建其他对象的父组),则每个下一个对象都显示为上一个子组。
您能帮我正确显示这些方法中的文件夹内容吗?