4

I've got implemented Drag and Drop from Finder to my app to NSTableView, and I created link to document, etc.

But, I want to make delete operation by drag item from NSTableView and drop this row onto Trash icon. How can I do this correctly? How enable drop onto trash?

Tomasz Szulc
  • 4,217
  • 4
  • 43
  • 79

2 Answers2

7

(It's been a long time since I've done this, and I'm doing it from memory and a glance at the docs. If this doesn't work, let me know and I'll double check w/ code.)

In draggingSession:sourceOperationMaskForDraggingContext: you should include NSDragOperationDelete as one of the legal operations. You will then receive NSDragOperationDelete back in your draggingSession:endedAtPoint:operation: to indicate that the item was dropped on the trash.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • I've got methods in `NSTableView` `- (void)tableView:(NSTableView *)tableView draggingSession:(NSDraggingSession *)session endedAtPoint:(NSPoint)screenPoint operation:(NSDragOperation)operation` but i've see that there i could only read that `operation == NSDragOperationDelete`, but i can't move it to trash. Icon is still the same, should be darker. On this method `- (NSDragOperation)tableView:(NSTableView *)tableView validateDrop:(id)info proposedRow:(NSInteger)row proposedDropOperation:(NSTableViewDropOperation)dropOperation ` I returned `NSDragOperationDelete. – Tomasz Szulc Jan 21 '13 at 23:07
  • I don't understand the comment "but I can't move it to trash." If you're receiving `NSDragOperationDelete`, then it's your job to delete the element from your data and tell the table to update. Can you describe the symptom more clearly? – Rob Napier Jan 22 '13 at 15:33
  • draggingSession:sourceOperationMaskForDraggingContext: This method is not called at dragging or droping time. That's why i did not receive any operation in method draggingSession:endedAtPoint:operation: In this method operation return me 0 Can you help me please ? – Surjeet Singh Sep 06 '13 at 14:15
  • Are you building for at least 10.7? If you're building for 10.6 or earlier, you need to use `draggingSourceOperationMaskForLocal:`. Read the Overview in https://developer.apple.com/library/mac/documentation/cocoa/reference/applicationkit/Protocols/NSDraggingSource_Protocol/Reference/Reference.html – Rob Napier Sep 06 '13 at 14:54
1

Use dropSessionDidEnd delegate method. there you can get the location of dropping point and there's no need for another UICollectionView/UITableView :

func collectionView(_ collectionView: UICollectionView, dropSessionDidEnd session: UIDropSession) {

  guard let item = session.items.first?.localObject as? YourObject else {
    return
  }
  let dropLocation = session.location(in: self.view)
  let itemDropedInTrash = TrashImageView.frame.contains(dropLocation)
  if itemDropedInTrash {
    //here update your datasource and reload your collectioView/tableView
    //deleteItemFromDataSource(item: item)
    //collectionView.reloadData()

  }
}
Mehrdad
  • 1,050
  • 1
  • 16
  • 27