Reputation: 2135
I've got NSOutlineView that, at the moment, only lists children of the root item. Everything works except when I try to rearrange items using drag & drop. The line with the circle in front of it always stays above the top row and doesn't follow my items when they need to be dragged between other items. However, I still get the index positions properly so I can still rearrange them in the datasource properly. I don't want to rearrange items by dragging them onto each other, I only want to rearrange the items off the root level (like the VLC playlist on the Mac).
Here are my four required methods:
//queue is a C-based data structure
-(NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
{
if(item == nil){
return queue.count; //each list item
}else{
return 0; //no children of each list item allowed
}
}
-(id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item
{
if(item != nil) return nil;
return [[NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithLong:index],@"index",
[NSString stringWithFormat:@"%s",queue.item[index].filepath],@"filePath",
nil] copy];
}
-(BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
{
if(item == nil){
return YES;
}else{
return NO;
}
}
-(id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
{
if(item == nil) return nil;
return [item objectForKey:[tableColumn identifier]];
}
And my drag methods:
-(BOOL)outlineView:(NSOutlineView *)outlineView writeItems:(NSArray *)items toPasteboard:(NSPasteboard *)pasteboard
{
[pasteboard declareTypes:[NSArray arrayWithObject:LOCAL_REORDER_PASTEBOARD_TYPE] owner:self];
[pasteboard setData:[NSData data] forType:LOCAL_REORDER_PASTEBOARD_TYPE];
return YES;
}
-(NSDragOperation)outlineView:(NSOutlineView *)outlineView validateDrop:(id <NSDraggingInfo>)info proposedItem:(id)item proposedChildIndex:(NSInteger)index
{
NSUInteger op = NSDragOperationNone;
if(index != NSOutlineViewDropOnItemIndex){
op = NSDragOperationMove;
}
return op;
}
Upvotes: 0
Views: 327
Reputation: 22948
I'm kind of confused about what desired behavior you want. Does this sample project achieve the desired drag and drop highlighting behavior (it shows the o––––––
indicator between each row, which is how VLC appears to work here for me):
http://www.markdouma.com/developer/NSOutlineViewFinagler.zip
(Note that it doesn't include code yet to actually carry through the reordering of the items).
If not, can you describe again what it currently does now and what you want it to do?
Also, you didn't include code for it here, but did you make sure to register the outline view for the LOCAL_REORDER_PASTEBOARD_TYPE
drag type? I would do it in awakeFromNib
:
- (void)awakeFromNib {
[self.outlineView registerForDraggedTypes:@[LOCAL_REORDER_PASTEBOARD_TYPE]];
}
That's necessary for the outline view to allow itself to be a drag destination.
Upvotes: 1