Expand item in NSOutlineView

  • Hello List,

    Can someone tell me how to expand the root node in an NSOutlineView (bound to an NSTreeController, managing entities) ?

    With "rootNode" being the topmost NSManagedObject in the hierarchy (i.e. having a "parent == nil" predicate), [treeView expandItem: [treeView rootNode]] simply does nothing and I think I'm missing out one something basic here...

    Thanks in advance for any helpful input !
    Alain
  • This one is a tricky one. Set in awakeFromNib or IB:

    [self setAutosaveName:@"MyOutline"];
    [self setAutosaveExpandedItems:YES];

    Then provide this delegate method so it can save the objects that are
    expanded:

    - (id)outlineView:(NSOutlineView *)outlineView
    persistentObjectForItem:(id)item {
        return [[[[item observedObject] objectID] URIRepresentation]
    absoluteString];
    }

    Then instead of letting it do it alone by providing the opposite delegate
    function - (id)outlineView:(NSOutlineView *)outlineView
    itemForPersistentObject:(id)object, I wrote my own expand routine and call
    it at applicationDidFinishLaunching with:

    [outlineView performSelector:@selector(expandURIs:)
    withObject:[preferencesStandard objectForKey:@"NSOutlineView Items
    MyOutline"] afterDelay:0.0];  //The Autosave name has to match

    The expand routine to put into the subclass of NSOutlineView. Here also
    watch out, this is my code as is, and it happens that my delegate is also
    the subclass, so the call to "[self outlineView:..." should really go to the
    delegate if it's a different object.

    // Expands the URI for the object given.
    // Is recursive if an object is found and others aren't as it might have
    been nested inside the one that was found.
    - (void)expandURIs:(NSArray *)someURIs {

        //Get items from the preferences
        NSMutableArray *notFoundNested = [NSMutableArray array];
        BOOL foundAtLeastOne = NO;
        NSEnumerator *collectionsToExpandEnum = [someURIs objectEnumerator];
        NSString *nextCollectionURI;

        while (nextCollectionURI = [collectionsToExpandEnum nextObject]) {
            int i, numberOfRows = [self numberOfRows];
            BOOL found = NO;

            for (i = 0; i < numberOfRows; i++ ) {

                if ([[self outlineView:self persistentObjectForItem:[self
    itemAtRow:i]] isEqualToString:nextCollectionURI]) {
                    [self expandItem:[self itemAtRow:i]];
                    foundAtLeastOne = YES;
                    found = YES;
                    break;
                }
            }

            if (found == NO) {
                [notFoundNested addObject:nextCollectionURI];
            }
        }

        if (foundAtLeastOne && [notFoundNested count])
            [self expandURIs:notFoundNested];
    }

    Regards,
    Conor
    http://www.bruji.com/
previous month august 2007 next month
MTWTFSS
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31    
Go to today