CollectionViewDataSource

@objc
public protocol CollectionViewDataSource

The CollectionViewDataSource is responsible for providing the data and views required by a collection view

Overview

At a minimum, all data source objects must implement:

  • numberOfSections(in:)
  • collectionView(_:numberOfItemsInSection:)
  • collectionView(_:cellForItemAt:).

These methods are responsible for returning the number of items in the collection view along with the items themselves.

  • Asks your data source for the number of sections in the collectin view

    Declaration

    Swift

    func numberOfSections(in collectionView: CollectionView) -> Int

    Parameters

    collectionView

    The collection view requesting this information.

    Return Value

    The number of sections in collectionView.

  • Asks your data source object for the number of items in the specified section.

    Declaration

    Swift

    func collectionView(_ collectionView: CollectionView, numberOfItemsInSection section: Int) -> Int

    Parameters

    collectionView

    The collection view requesting this information.

    section

    An index number identifying a section in collectionView. This index value is 0-based.

    Return Value

    The number of items in the specified section

  • Asks your data source object for the cell that corresponds to the specified item in the collection view.

    Discussion

    Your implementation of this method is responsible for creating, configuring, and returning the appropriate cell for the given item. You do this by calling the dequeueReusableCell(withReuseIdentifier:for:) method of the collection view and passing the reuse identifier that corresponds to the cell type you want. That method always returns a valid cell object. Upon receiving the cell, you should set any properties that correspond to the data of the corresponding item, perform any additional needed configuration, and return the cell. You do not need to set the location of the cell inside the collection view’s bounds. The collection view sets the location of each cell automatically using the layout attributes provided by its layout object.

    Declaration

    Swift

    func collectionView(_ collectionView: CollectionView, cellForItemAt indexPath: IndexPath) -> CollectionViewCell

    Parameters

    collectionView

    The collection view requesting this information.

    indexPath

    The index path that specifies the location of the item.

    Return Value

    A configured cell object. You must not return nil from this method.

  • Asks your data source object to provide a supplementary view to display in the collection view.

    Discussion

    Your implementation of this method is responsible for creating, configuring, and returning the appropriate supplementary view that is being requested. You do this by calling the dequeueReusableSupplementaryView(ofKind:withReuseIdentifier:for:) method of the collection view and passing the information that corresponds to the view you want. That method always returns a valid view object. Upon receiving the view, you should set any properties that correspond to the data you want to display, perform any additional needed configuration, and return the view. You do not need to set the location of the supplementary view inside the collection view’s bounds. The collection view sets the location of each view using the layout attributes provided by its layout object.

    Declaration

    Swift

    @objc
    optional func collectionView(_ collectionView: CollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> CollectionReusableView

    Parameters

    collectionView

    The collection view requesting this information.

    kind

    The kind of supplementary view to provide. The value of this string is defined by the layout object that supports the supplementary view.

    indexPath

    The index path that specifies the location of the new supplementary view.

    Return Value

    A configured supplementary view object. You must not return nil from this method.

  • Asks your data source for a pasteboard writing for the item at the specified index path

    Declaration

    Swift

    @objc
    optional func collectionView(_ collectionView: CollectionView, pasteboardWriterForItemAt indexPath: IndexPath) -> NSPasteboardWriting?

    Parameters

    collectionView

    The collection view requesting this information.

    indexPath

    The index path of the item to represent with the 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.

  • Asks the data source for the drag contents for the item at the specified index path

    Note

    If nil is returned, a snapshot of the cell will be used. To disable dragging for an item return false for shouldStartDragging or remove the index path during validation

    Declaration

    Swift

    @objc
    optional func collectionView(_ collectionView: CollectionView, dragContentsForItemAt indexPath: IndexPath) -> NSImage?

    Parameters

    collectionView

    The collection view requesting this information.

    indexPath

    The index path of the item to represent

    Return Value

    An NSImage to display when dragging the item

  • Asks the data source to validate the drag rect for an item to be dragged, allowing for adjustment.

    Declaration

    Swift

    @objc
    optional func collectionView(_ collectionView: CollectionView, dragRectForItemAt indexPath: IndexPath, withStartingRect rect: UnsafeMutablePointer<CGRect>)

    Parameters

    collectionView

    The collection view requesting this information.

    indexPath

    The index path of the item being dragged

    rect

    The current rect of the item