Mac Developer Library

Developer

AppKit Framework Reference NSCollectionViewDelegate Protocol Reference

Options
Deployment Target:

On This Page
Language:

NSCollectionViewDelegate

The NSCollectionViewDelegate protocol defines the optional methods implemented by delegates of NSCollectionView objects. More...

Inheritance


Not Applicable

Import Statement


import AppKit @import AppKit;

Availability


Available in OS X v10.6 and later.
  • Returns whether the collection view can attempt to initiate a drag for the given event and items.

    Declaration

    Swift

    optional func collectionView(_ collectionView: NSCollectionView, canDragItemsAtIndexes indexes: NSIndexSet, withEvent event: NSEvent) -> Bool

    Objective-C

    - (BOOL)collectionView:(NSCollectionView *)collectionView canDragItemsAtIndexes:(NSIndexSet *)indexes withEvent:(NSEvent *)event

    Parameters

    collectionView

    The collection view that send the message.

    indexes

    The indexes of the proposed dragging items.

    event

    The mouse down event that initiated the drag.

    Return Value

    YEStrue if the items can attempt to initiate a drag for the specified items, otherwise NOfalse.

    Discussion

    If the delegate does not implement this method, the collection view will act as if it returned YEStrue.

    Import Statement

    import AppKit

    Availability

    Available in OS X v10.6 and later.

  • Invoked to determine a valid drop target.

    Declaration

    Swift

    optional func collectionView(_ collectionView: NSCollectionView, validateDrop draggingInfo: NSDraggingInfo, proposedIndex proposedDropIndex: UnsafeMutablePointer<Int>, dropOperation proposedDropOperation: UnsafeMutablePointer<NSCollectionViewDropOperation>) -> NSDragOperation

    Objective-C

    - (NSDragOperation)collectionView:(NSCollectionView *)collectionView validateDrop:(id<NSDraggingInfo>)draggingInfo proposedIndex:(NSInteger *)proposedDropIndex dropOperation:(NSCollectionViewDropOperation *)proposedDropOperation

    Parameters

    collectionView

    The collection view that send the message.

    draggingInfo

    An object containing details about this dragging operation.

    proposedDropIndex

    The proposed drop index. This parameter is passed by-reference and can be modified retarget the drop operation.

    proposedDropOperation

    The proposed drop operation. This parameter is passed by-reference and can be modified to change the drop operation.

    Return Value

    A value that indicates which dragging operation the data source will perform. It must return something other than NSDragOperationNone to accept the drop

    Discussion

    Based on the mouse position, the collection view will suggest a proposed index and drop operation. These values are in/out parameters and can be changed by the delegate to retarget the drop operation.

    The collection view will propose NSCollectionViewDropOn when the dragging location is closer to the middle of the item than either of its edges. Otherwise, it will propose NSCollectionViewDropBefore. You may override this default behavior by changing proposedDropOperation or proposedDropIndex.

    To receive drag messages, you must first send registerForDraggedTypes: to the collection view with the drag types you want to support.

    You must implement this method for your collection view to be a drag destination.

    Import Statement

    import AppKit

    Availability

    Available in OS X v10.6 and later.

  • Invoked when the mouse is released over a collection view that previously allowed a drop.

    Declaration

    Swift

    optional func collectionView(_ collectionView: NSCollectionView, acceptDrop draggingInfo: NSDraggingInfo, index index: Int, dropOperation dropOperation: NSCollectionViewDropOperation) -> Bool

    Objective-C

    - (BOOL)collectionView:(NSCollectionView *)collectionView acceptDrop:(id<NSDraggingInfo>)draggingInfo index:(NSInteger)index dropOperation:(NSCollectionViewDropOperation)dropOperation

    Parameters

    collectionView

    The collection view that send the message.

    draggingInfo

    An object that contains more information about this dragging operation.

    index

    The index of the proposed drop item.

    dropOperation

    The type of dragging operation.

    Return Value

    YEStrue if the drop operation should be accepted, otherwise NOfalse.

    Discussion

    This method is called when the mouse is released over a collection view that previously decied to allow a drop via the collectionView:validateDrop:proposedIndex:dropOperation: method. At this time, the delegate should incorporate the data from the dragging pasteboard and update the collection view's contents.

    You must implement this method for your collection view to be a drag destination

    Import Statement

    import AppKit

    Availability

    Available in OS X v10.6 and later.

  • Sent to the delegate to allow creation of a custom image to represent collection view items during a drag operation.

    Declaration

    Swift

    optional func collectionView(_ collectionView: NSCollectionView, draggingImageForItemsAtIndexes indexes: NSIndexSet!, withEvent event: NSEvent!, offset dragImageOffset: NSPointPointer) -> NSImage!

    Objective-C

    - (NSImage *)collectionView:(NSCollectionView *)collectionView draggingImageForItemsAtIndexes:(NSIndexSet *)indexes withEvent:(NSEvent *)event offset:(NSPointPointer)dragImageOffset

    Parameters

    collectionView

    The collection view that send the message.

    indexes

    The indexes of the dragging items.

    event

    The mouse down event that initiated the drag.

    dragImageOffset

    An in/out parameter that will initially be set to NSZeroPoint. it can be modified to reposition the returned image. A dragImageOffset of NSZeroPoint will cause the image to be centered under the mouse.

    Return Value

    An image containing a rendering of the visible portions of the views for each item.

    Discussion

    If the delegate does not implement this method, the collection view with return an image using draggingImageForItemsAtIndexes:withEvent:offset:. You can safely invoked draggingImageForItemsAtIndexes:withEvent:offset: on collectionView from within this method.

    You do not need to implement this method for your collection view to be a drag source.

    Import Statement

    import AppKit

    Availability

    Available in OS X v10.6 and later.

  • Invoked to return an array of filenames that the receiver promises to create.

    Declaration

    Swift

    optional func collectionView(_ collectionView: NSCollectionView, namesOfPromisedFilesDroppedAtDestination dropURL: NSURL!, forDraggedItemsAtIndexes indexes: NSIndexSet!) -> [AnyObject]!

    Objective-C

    - (NSArray *)collectionView:(NSCollectionView *)collectionView namesOfPromisedFilesDroppedAtDestination:(NSURL *)dropURL forDraggedItemsAtIndexes:(NSIndexSet *)indexes

    Parameters

    collectionView

    The collection view that send the message.

    dropURL

    The drop location where the files are created.

    indexes

    The indexes of the dragging items.

    Return Value

    An array of filenames (not full paths) for the created files that the receiver promises to create.

    Discussion

    The delegate can support file promise drags by adding NSFilesPromisePboardType to the pasteboard in collectionView:writeItemsAtIndexes:toPasteboard:.

    For more information on file promise dragging, see documentation for the NSDraggingSource protocol and namesOfPromisedFilesDroppedAtDestination:.

    You do not need to implement this delegate method for your collection view to be a drag source.

    Import Statement

    import AppKit

    Availability

    Available in OS X v10.6 and later.

  • Invoked after it has been determined that a drag should begin, but before the drag has been started.

    Declaration

    Swift

    optional func collectionView(_ collectionView: NSCollectionView, writeItemsAtIndexes indexes: NSIndexSet, toPasteboard pasteboard: NSPasteboard) -> Bool

    Objective-C

    - (BOOL)collectionView:(NSCollectionView *)collectionView writeItemsAtIndexes:(NSIndexSet *)indexes toPasteboard:(NSPasteboard *)pasteboard

    Parameters

    collectionView

    The collection view that send the message.

    indexes

    The indexes of the items to write to the pasteboard.

    pasteboard

    The pasteboard containing the content from the dragged items.

    Return Value

    YEStrue to begin the drag, otherwise NOfalse.

    Discussion

    To start the drag, you must first declare the pasteboard types that are supported by sending pasteboard a declareTypes:owner: method. You then place the data for the items at the specified indexes on pasteboard, and return YEStrue from the method.

    The drag image and other drag related information will be set up and provided by the view once this call returns YEStrue.

    You need to implement this method for your collection view to be a drag source.

    Import Statement

    import AppKit

    Availability

    Available in OS X v10.6 and later.