collectionView(_:pasteboardWriterForItemAt:)
Provides the pasteboard writer for the item at the specified index path.
Declaration
@MainActor optional func collectionView(_ collectionView: NSCollectionView, pasteboardWriterForItemAt indexPath: IndexPath) -> (any NSPasteboardWriting)?Parameters
- collectionView:
The collection view making the request.
- indexPath:
The index path of the item requiring a pasteboard writer.
Return Value
The pasteboard writer object to use for managing the item data. Return nil to prevent the collection view from dragging the item.
Discussion
You must implement this method or the collectionView(_:writeItemsAt:to:) method to support drag operations. The collection view calls this method in preference over the collectionView(_:writeItemsAt:to:) method if both are implemented. If your app supports multi-image drag and drop, you must implement this method.
The collection view calls this method for each item involved in the drag operation after it has determined that a drag should begin but before the drag operation has started. Your implementation of this method should create and return the pasteboard writer—an object conforming to the NSPasteboardWriting protocol—to use for providing the item’s data. Using the object you provide, the collection view creates an NSDraggingItem object for you and configures its draggingFrame and imageComponents properties for you using information from the item at the specified index path.
If you implement this method, the collection view does not call the collectionView(_:draggingImageForItemsAt:with:offset:) of your delegate or the draggingImageForItems(at:with:offset:) method of NSCollectionView.
See Also
Drag and Drop Support
Supporting Collection View Drag and Drop Through File PromisescollectionView(_:canDragItemsAt:with:)collectionView(_:writeItemsAt:to:)collectionView(_:namesOfPromisedFilesDroppedAtDestination:forDraggedItemsAt:)collectionView(_:draggingImageForItemsAt:with:offset:)collectionView(_:draggingSession:willBeginAt:forItemsAt:)collectionView(_:draggingSession:endedAt:dragOperation:)collectionView(_:updateDraggingItemsForDrag:)collectionView(_:validateDrop:proposedIndexPath:dropOperation:)collectionView(_:acceptDrop:indexPath:dropOperation:)