I recently began exploring NSTreeController, but I'm experiencing
problems with its moveNode:toIndexPath:. It's new to Leopard, but
seems to be consistently moving nodes to incorrect indexes. There's a
previous list post about exactly this, but without replies. 
http://lists.apple.com/archives/cocoa-dev/2008/Sep/msg01635.html

The same thing happens with Apple's own sample code, SourceView! 
(http://developer.apple.com/samplecode/SourceView
)
-[BaseNode setChildren:] gets called twice for every time you move
something in the sidebar, once for removing the item from its previous
location, once for inserting it in the new location. When inserting,
the new item is always inserted _last_ among its siblings, no matter
what index you dropped it on. Items are correctly ordered in the
OutlineView itself, but the underlying model is incorrect, and thus
out of sync.


My experience is that this problem occurs at the top level, ie, where the node's proposed parent is nil. In that case I found that creating the index path with +indexPathWithIndex: produces the right result, whereas -indexPathByAddingIndex: gives the result you are seeing. So I put in a conditional statement as below, and it all seems to work fine.

- (BOOL)outlineView:(NSOutlineView *)outlineView
                 acceptDrop:(id <NSDraggingInfo>)info
                           item:(id)newParent
                 childIndex:(NSInteger)index
{
/*---Drop the dragged nodes such that 'newParent' (which may be nil) becomes their parent and 'index' the position (among their fellow children) of the first dragged node with the others immediately below it. */
        NSIndexPath *dropPath;
        NSInteger dropIndex;
        
        dropIndex = ( index == NSOutlineViewDropOnItemIndex ) ? 0 : index;
dropPath = ( newParent ) ? [[newParent indexPath] indexPathByAddingIndex:dropIndex] : [NSIndexPath indexPathWithIndex:dropIndex];

        [treeController moveNodes:draggedNodes toIndexPath:dropPath];


Not sure if your situation is exactly the same, but I hope this helps.
Jon
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to