NSTreeController -removeObjectsAtArrangedObjectIndexPath:

  • Has any one else had issues with this method?  I have an outline view
    fed by a tree controller.  When a root level item is deleted I'd like
    to delete the child items as well (or any parent item for that
    matter).  My first try was to simply call
    [outlineViewContentController
    removeObjectsAtArrangedObjectIndexPaths:paths]; where paths is an
    array of index paths to valid items including both the parent item
    and all descendant items.  When I do that I get an exception on
    NSArray -addObject: saying that I am adding a nil object.  Somewhat
    confusing seeing as I'm removing objects, but we'll chalk that up to
    the oddness of NSOutlineView/NSTreeController.  So I decide to take
    this apart and delete each level of items in reverse order... leaf
    nodes first, root nodes last.  40 lines of code later it works.  But
    Cocoa shouldn't be this way and I'd like a better way.  Does anyone
    know why -removeObjectsAtArrangedObjectIndexPath: doesn't work with
    multiple levels of item index paths?  Is there a less complex work
    around than going through the paths, and sorting them by length
    (depth) and processing them in batches?

    Thanks,

    ->Ben
  • I don't know this method.  I'm not sure what the docs mean by "indexPaths".

    I'm doing much the same thing (removing a tree of items from a OutlineView)
    with a small recursive function.  To wit:

    - (void)clearTree {
        int i;
        int kids = [self numberOfChildren];
        for (i=kids-1; i>=0; i--)
            [[self childAtIndex:i] clearTree];
        id tmp = [self children];
        if (tmp != IsALeafNode) [children release];
        children == NULL;
    }

    The asking the parent item to reload...

    On 10/7/07 9:46 PM, "Ben Lachman" <blachman...> wrote:

    > Is there a less complex work
    > around than going through the paths, and sorting them by length
    > (depth) and processing them in batches?
  • After looking at this all morning and reading various docs, cocoa-
    dev, old posts, etc. I came up with a method that no one mentions,
    probably because it is so simple if you're using a coredata backing.
    All you do is use the basic removeObjectsAtArrangedObjectIndexPath:
    on the set of selection indexes and set the delete rule for your
    coredata object's child relationship to cascade deletes.  Just make
    sure you commitEditing and rearrangeObjects on the tree controller
    and reloadData on the outline view after doing this.  Another nice
    byproduct of this is that it gets rid of a bug where removed items
    come back if you rename another item in the outlineview (gosh I hate
    NSOutlineView/NSTreecontroller).

    Hope this helps someone out.

    ->Ben
    --
    "Pinky, you've left the lens cap of your mind on again." - The Brain

    On Oct 8, 2007, at 2:48 PM, Chris Williams wrote:

    > I don't know this method.  I'm not sure what the docs mean by
    > "indexPaths".
    >
    > I'm doing much the same thing (removing a tree of items from a
    > OutlineView)
    > with a small recursive function.  To wit:
    >
    > - (void)clearTree {
    > int i;
    > int kids = [self numberOfChildren];
    > for (i=kids-1; i>=0; i--)
    > [[self childAtIndex:i] clearTree];
    > id tmp = [self children];
    > if (tmp != IsALeafNode) [children release];
    > children == NULL;
    > }
    >
    > The asking the parent item to reload...
    >
    >
    > On 10/7/07 9:46 PM, "Ben Lachman" <blachman...> wrote:
    >
    >> Is there a less complex work
    >> around than going through the paths, and sorting them by length
    >> (depth) and processing them in batches?
    >
  • Index paths are a way to specify where you are in a data hierarchy.  The
    NSIndexPath documentation has a good illustration of this.  An index path is
    simply an array of each of the array indices that lead to the desired
    location.  There are methods to remove last index to move up in the
    hierarchy or add an index to move down.

        My only gripe with NSIndexPath is that it doesn't allow an empty path,
    making for extra lines of code when manipulating things at the top level.
    For example, moving within the current level by removing an index and then
    adding new index does not work at the top level.  You have to handle it as a
    special case.

    > I don't know this method.  I'm not sure what the docs mean by "indexPaths".
    >
    > I'm doing much the same thing (removing a tree of items from a OutlineView)
    > with a small recursive function.  To wit:
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