NSCollectionViewDelegate Protocol Reference

Conforms to
Framework
/System/Library/Frameworks/AppKit.framework
Availability
Available in OS X v10.6 and later.
Declared in
NSCollectionView.h
Related sample code

Overview

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

The NSCollectionViewDelegate provides support for both drag and drop, and pasteboard support to collection views.

Tasks

Drag and Drop Support

Instance Methods

collectionView:acceptDrop:index:dropOperation:

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

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

YES if the drop operation should be accepted, otherwise NO.

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

Availability
  • Available in OS X v10.6 and later.
Declared In
NSCollectionView.h

collectionView:canDragItemsAtIndexes:withEvent:

Returns whether the collection view can attempt to initiate a drag for the given event and items.

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

YES if the items can attempt to initiate a drag for the specified items, otherwise NO.

Discussion

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

Availability
  • Available in OS X v10.6 and later.
Declared In
NSCollectionView.h

collectionView:draggingImageForItemsAtIndexes:withEvent:offset:

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

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

Availability
  • Available in OS X v10.6 and later.
Declared In
NSCollectionView.h

collectionView:namesOfPromisedFilesDroppedAtDestination:forDraggedItemsAtIndexes:

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

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

Availability
  • Available in OS X v10.6 and later.
Declared In
NSCollectionView.h

collectionView:validateDrop:proposedIndex:dropOperation:

Invoked to determine a valid drop target.

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

Availability
  • Available in OS X v10.6 and later.
Declared In
NSCollectionView.h

collectionView:writeItemsAtIndexes:toPasteboard:

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

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

YES to begin the drag, otherwise NO.

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 YES from the method.

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

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

Availability
  • Available in OS X v10.6 and later.
Declared In
NSCollectionView.h