Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable drop something on the Trash in objective-c?

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?

like image 399
Tomasz Szulc Avatar asked Feb 17 '23 18:02

Tomasz Szulc


2 Answers

(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.

like image 181
Rob Napier Avatar answered Mar 03 '23 02:03

Rob Napier


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()

  }
}
like image 23
Mehrdad Avatar answered Mar 03 '23 01:03

Mehrdad