NSTreeController の使用方法を理解しようとしています。NSTreeController を介して NSOutlineView に送信されるディレクトリの内容を反復処理するメソッド (SourceView の例のクラスとメソッドの一部を以下に示します) を追加すると、ただし、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 の例では、他のオブジェクトの親グループを作成するために使用されます)、次のすべてのオブジェクトが前のサブグループとして表示されます。
これらの方法でフォルダの内容を正しく表示するのを手伝ってもらえますか?