Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dragging out of NSTableView to Remove

I have an NSTableView that contains a number of items. I'd like to implement dragging from inside and dropping outside of the NSTableView to delete the dragged item. (Kind of like how Safari 'poofs' bookmarks away.)

The NSTableView is already configured to support drag n' drop reordering, and accepts drops from another NSTableView in the application, so while most of it is wired up, I'm just missing a small piece of functionality.

Update:

Thanks to Sean for his help so far. While I can receive notification that an item has been dragged out after it has floated its way back to its original position (see comment to his answer), I'd like to receive notification immediately once the mouse button is released.

The current behavior is for the user to drag-drop an item out, the drag to be treated as "not valid," and for the item to animate back to its original position. Then the notification is received, and I'm able to remove the item, but it's a little confusing (for users) from a UI standpoint.

like image 654
Craig Otis Avatar asked Oct 09 '11 22:10

Craig Otis


2 Answers

It sounds like your table view is already implementing the NSDraggingSource protocol. In this case you can add the method draggedImage:endedAt:operation: (pre OS 10.7) or draggingSession:endedAtPoint:operation: (OS 10.7) which will supply the operation NSDragOperationNone in the operation argument if the drag failed. Thus, you can check if this operation was passed, then delete the object and remove it from the view if so.

In other words, it would look something like this (in 10.7) :

-(void)draggingSession:(NSDraggingSession *)session endedAtPoint:(NSPoint)screenPoint operation:(NSDragOperation)operation {
    if (operation == NSDragOperationNone) {
        //delete object, remove from view, etc.
    }
}
like image 171
Sean Avatar answered Oct 21 '22 01:10

Sean


To avoid the drag image floating back to its original location before the deletion, you can set the dragging session property "animatesToStartingPositionsOnCancelOrFail" in the tableView:draggingSession:willBeginAtPoint:forRowIndexes: method:

- (void)tableView:(NSTableView *)tableView draggingSession:(NSDraggingSession *)session willBeginAtPoint:(NSPoint)screenPoint forRowIndexes:(NSIndexSet *)rowIndexes {
   [session setAnimatesToStartingPositionsOnCancelOrFail:NO];
}

However I am still searching for method to create the "poof" when deleting a dragged row.

like image 43
SarahR Avatar answered Oct 21 '22 00:10

SarahR