Protocol

NSCollectionViewDelegate

The NSCollectionViewDelegate protocol defines methods for managing the behavior of a collection view. You use the methods of this protocol to facilitate the user-initiated selection and highlighting of items, to track changes to the collection view’s visual elements, and to implement drag and drop support. The methods of this protocol are optional, but for some features, you must implement specific methods to support the feature.

Overview

Implement the methods of this protocol in an object that you use to manage your collection view. Typically, you implement delegate support in the view controller or window controller that manages the collection view itself, but you can implement these methods in another object if you prefer. Assign your delegate object to the collection view either programmatically (by setting the value of the collection view’s delegate property) or at design time in Interface Builder.

To implement drag and drop support in your collection view, implement the following methods:

For more information about handling drag and drop operations, see Drag and Drop Programming Topics.

Legacy Support

Before OS X 10.11, collection views supported only a single section of items organized in a grid layout. The drag and drop methods of this protocol include variants that take a single index or an NSIndexSet as a parameter. Although you can use those methods to implement your drag and drop support, it is recommended that you use the newer methods that take NSIndexPath objects instead.

Symbols

Managing the Selection

func collectionView(NSCollectionView, shouldSelectItemsAt: Set<IndexPath>)

Asks the delegate to approve the pending selection of items.

func collectionView(NSCollectionView, didSelectItemsAt: Set<IndexPath>)

Notifies the delegate object that one or more items were selected.

func collectionView(NSCollectionView, shouldDeselectItemsAt: Set<IndexPath>)

Asks the delegate object to approve the pending deselection of items.

func collectionView(NSCollectionView, didDeselectItemsAt: Set<IndexPath>)

Notifies the delegate object that one or more items were deselected.

Managing Item Highlighting

func collectionView(NSCollectionView, shouldChangeItemsAt: Set<IndexPath>, to: NSCollectionViewItemHighlightState)

Asks the delegate to approve the pending highlighting of the specified items.

func collectionView(NSCollectionView, didChangeItemsAt: Set<IndexPath>, to: NSCollectionViewItemHighlightState)

Notifies the delegate that the highlight state of the specified items changed.

Tracking the Addition and Removal of Items

func collectionView(NSCollectionView, willDisplay: NSCollectionViewItem, forRepresentedObjectAt: IndexPath)

Notifies the delegate that the specified item is about to be displayed by the collection view.

func collectionView(NSCollectionView, willDisplaySupplementaryView: NSView, forElementKind: String, at: IndexPath)

Notifies the delegate that the specified supplementary view is about to be displayed by the collection view.

func collectionView(NSCollectionView, didEndDisplaying: NSCollectionViewItem, forRepresentedObjectAt: IndexPath)

Notifies the delegate that the specified item was removed from the collection view.

func collectionView(NSCollectionView, didEndDisplayingSupplementaryView: NSView, forElementOfKind: String, at: IndexPath)

Notifies the delegate that the specified supplementary view was removed from the collection view.

Handling Layout Changes

func collectionView(NSCollectionView, transitionLayoutForOldLayout: NSCollectionViewLayout, newLayout: NSCollectionViewLayout)

Returns the transition layout object to use when performing an animated change between different layouts.

Drag and Drop Support

func collectionView(NSCollectionView, canDragItemsAt: Set<IndexPath>, with: NSEvent)

Returns a Boolean indicating whether a drag operation involving the specified items can begin.

func collectionView(NSCollectionView, pasteboardWriterForItemAt: IndexPath)

Provides the pasteboard writer for the item at the specified index path.

func collectionView(NSCollectionView, writeItemsAt: Set<IndexPath>, to: NSPasteboard)

Places the data for the drag operation on the pasteboard.

func collectionView(NSCollectionView, draggingImageForItemsAt: Set<IndexPath>, with: NSEvent, offset: NSPointPointer)

Creates and returns a drag image to represent the specified items during a drag.

func collectionView(NSCollectionView, updateDraggingItemsForDrag: NSDraggingInfo)

Asks your delegate to update the dragging items during a drag operation.

Legacy Collection View Support

These methods are called only on a collection view that has only one section. They are not called for collection views with two or more sections.

func collectionView(NSCollectionView, canDragItemsAt: IndexSet, with: NSEvent)

Returns a Boolean indicating whether the collection view can begin dragging the specified items.

func collectionView(NSCollectionView, pasteboardWriterForItemAt: Int)

Provides the pasteboard writer for the item at the specified index

func collectionView(NSCollectionView, writeItemsAt: IndexSet, to: NSPasteboard)

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

func collectionView(NSCollectionView, namesOfPromisedFilesDroppedAtDestination: URL, forDraggedItemsAt: IndexSet)

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

func collectionView(NSCollectionView, draggingImageForItemsAt: IndexSet, with: NSEvent, offset: NSPointPointer)

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

func collectionView(NSCollectionView, acceptDrop: NSDraggingInfo, index: Int, dropOperation: NSCollectionViewDropOperation)

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

Relationships

Inherits From