Re: NSTreeController and insertObject:atIndex question

  • I'm no expert, as I've proven many times here, but I'll offer the
    following anyway since nobody else has stepped up to it.  At least if I say
    something wrong, somebody will usually set it straight.  However, this works
    for me.

        What I use to insert objects into the array is to use "arrangedObjects"
    and an appropriate path.  In my case, each level of the tree corresponds to
    a particular class of object in my data hierarchy.  The first thing I have
    to do are determine the level of the selected insert point, which determines
    what class of inserted object I am dealing with.  In your case, if the
    levels are all the same class, then you don't have to do the latter.  It
    sounds like in your case the level is either 0 or 1, depending on how you
    specified your root object.

        You can get the path from
            (NSIndexPath *)selectionIndexPath

        then use
            (NSIndexPath *)indexPathByRemovingLastIndex

        then specify wherever you want it inserted at that level using
            NSIndexPath *)indexPathByAddingIndex:(unsigned int)index

        then insert the object using
            insertObject:(id)object atArrangedObjectIndexPath:(NSIndexPath
    *)indexPath

            (Note that at the top level, "NSIndexPath
    *)indexPathByAddingIndex:(unsigned int)index" doesn't work.  NSIndexPath
    doesn't like an empty path.)  I had to work around that one.

        In your case, you can probably skip most of that and just set the path
    directly with " [[NSIndexPath alloc] initWithIndex: 0], then insert it if
    you want it in the top level of your tree.

    --
    Gordon Apple
    Ed4U
    Little Rock, AR
    <ga...>

    > I have a NSTreeController. Its content is bound to my document class
    > to get the NSArray behind the top level of the tree. When my
    > windowController gets a command to import a movie I do the following..
    >
    > -(IBAction)importMovie:(id)sender
    > {
    > NSMutableArray* array = [self
    > mutableArrayValueForKeyPath:@"document.project"];
    >
    > if (array != nil)
    > {
    > NSOpenPanel* openPanel = [NSOpenPanel openPanel];
    >
    > if ([openPanel runModalForTypes:[QTMovie movieFileTypes:0]] ==
    > NSOKButton)
    > {
    > QTMovie* movie = [QTMovie movieWithFile:[openPanel filename]
    > error:NULL];
    > VAMovieNode* movieNode = [VAMovieNode withMovie:movie];
    >
    > [array insertObject:movieNode atIndex:0];
    > }
    > }
    >
    > }
    >
    > Then my document has the following call to do some document specific
    > stuff, right now its stubbed... The idea behind this is that the movie
    > can create the basic items but there are some very specific things,
    > like undo, that the document can handle.
    >
    > - (void)insertObject:(VATreeNode *)transaction
    > inProjectAtIndex:(unsigned int)index
    > {
    > // implementation specific code
    > return;
    > }
    >
    > I have also tried..
    >
    > -(void)insertProject:(NSArray *)objects atIndexes:(NSIndexSet*)index
    >
    > However when i place break points into these calls they are never
    > hit.. Are the insert calls different for NSTreeController?
    >
    > Scott Andrew
    >
previous month october 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