Как правильно отображать перечисленные объекты с помощью NSTreeController

Я пытаюсь понять, как использовать 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 не различает простые объекты (файлы) и каталоги (папки).

Однако, когда я заполняю NSOutlineView без NSTreeController, все содержимое отображается правильно:

ПРИМЕЧАНИЕ. Если используется метод addFolder: (в примере с SourceView он используется для создания родительских групп для других объектов), то каждый следующий объект отображается как подгруппа предыдущего.

Можете ли вы помочь мне правильно отобразить содержимое папки в этих методах?

0 ответов

Другие вопросы по тегам