How to create NSPasteboardWriting for drag and drop in NSCollectionView

I have a one-sector view of the collection and you want to implement Drag and Drop to allow reordering of items. CollectionViewItemhas several text views showing the properties that form my Parameter objects. Reading a document I need to implement NSCollectionView delegate:

func collectionView(_ collectionView: NSCollectionView, pasteboardWriterForItemAt indexPath: IndexPath) -> NSPasteboardWriting? {
    let parameter = parameterForIndexPath(indexPath: indexPath)
    return parameter // throws an error "Cannot convert return expression of type 'Parameter' to return type 'NSPasteboardWriting?'"
}

I did not find any information that I understand, describing the nature of the object NSPasteboardWriting. So, I don’t know how to proceed ... What is an object NSPasteboardWritingand what do I need to write in cardboard?

Thank!

+4
source share
1 answer

: - , , , , . , , , !

"handboardwriter" (, , ) - , , . .

, , . , acceptDrop ( ). , .

. sourcelist, .

  • in viewDidLoad() sourcelist, . , , .

    // Register for the dropped object types we can accept. sourceList.register(forDraggedTypes: [REORDER_SOURCELIST_PASTEBOARD_TYPE])

    REORDER_SOURCELIST_PASTEBOARD_TYPE, , :

       `let REORDER_SOURCELIST_PASTEBOARD_TYPE = "com.yourdomain.sourcelist.item"`
    

    ... - , .. yourdomain - , com.myapp.sourcelist.item.

    ( ):

       import Cocoa
    
       let REORDER_SOURCELIST_PASTEBOARD_TYPE = "com.yourdomain.sourcelist.item"`
    
       class Something {
          // ...etc...
    
  • view pasteboardWriterForItem.. (, sourcelist, - ). sourcelist :

      // Return a pasteboard writer if this outlineview item should be able to 
      // drag-and-drop.
      func outlineView(_ outlineView: NSOutlineView, pasteboardWriterForItem item: Any) -> NSPasteboardWriting? {
        let pbItem = NSPasteboardItem()
    
        // See if the item is of the draggable kind. If so, set the pasteboard item.
        if let draggableThing = ((item as? NSTreeNode)?.representedObject) as? DraggableThing {
              pbItem.setString(draggableThing.uuid, forType: REORDER_SOURCELIST_PASTEBOARD_TYPE)
              return pbItem;
        }
    
        return nil
      }
    

    draggableThing.uuid, , .

  • , , , , .

    func outlineView(_ outlineView: NSOutlineView, validateDrop info: NSDraggingInfo, proposedItem item: Any?, proposedChildIndex index: Int) -> NSDragOperation {
        // Get the pasteboard types that this dragging item can offer. If none
        // then bail out.
        guard let draggingTypes = info.draggingPasteboard().types else {
            return []
        }
    
        if draggingTypes.contains(REORDER_SOURCELIST_PASTEBOARD_TYPE) {
            if index >= 0 && item != nil {
                return .move
            }
          }
    
          return []
        }
    
  • . , () .

    func outlineView(_ outlineView: NSOutlineView, acceptDrop info: NSDraggingInfo, item: Any?, childIndex index: Int) -> Bool {
      let pasteboard = info.draggingPasteboard()
    
      let uuid = pasteboard.string(forType: REORDER_SOURCELIST_PASTEBOARD_TYPE)
    
      // Go through each of the tree nodes, to see if this uuid is one of them.
      var sourceNode: NSTreeNode?
      if let item = item as? NSTreeNode, item.children != nil {
        for node in item.children! {
            if let collection = node.representedObject as? Collection {
                if collection.uuid == uuid {
                    sourceNode = node
                }
            }
        }
      }
    
      if sourceNode == nil {
        return false
      }
    
      // Reorder the items.
      let indexArr: [Int] = [1, index]
      let toIndexPath = NSIndexPath(indexes: indexArr, length: 2)
      treeController.move(sourceNode!, to: toIndexPath as IndexPath)
    
      return true
    }
    

: Cocoa, , , - , (.. ), ! , , , , , .

+4

Source: https://habr.com/ru/post/1670167/


All Articles