iOS Developer Library

Developer

UIKit Framework Reference UICollectionViewDataSource Protocol Reference

Options
Deployment Target:

On This Page
Language:

UICollectionViewDataSource

An object that adopts the UICollectionViewDataSource protocol is responsible for providing the data and views required by a collection view. A data source object represents your app’s data model and vends information to the collection view as needed. It also handles the creation and configuration of cells and supplementary views used by the collection view to display your data.

At a minimum, all data source objects must implement the collectionView:numberOfItemsInSection: and collectionView:cellForItemAtIndexPath: methods. These methods are responsible for returning the number of items in the collection view along with the items themselves. The remaining methods of the protocol are optional and only needed if your collection view organizes items into multiple sections or provides headers and footers for a given section.

When configuring the collection view object, assign your data source to its dataSource property. For more information about how a collection view works with its data source to present content, see UICollectionView Class Reference.

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

    Declaration

    Swift

    func collectionView(_ collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell

    Objective-C

    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

    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.

    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 dequeueReusableCellWithReuseIdentifier:forIndexPath: 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.

    This method must always return a valid view object.

    Availability

    Available in iOS 6.0 and later.

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

    Declaration

    Swift

    optional func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView

    Objective-C

    - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath

    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.

    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 dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath: 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.

    This method must always return a valid view object. If you do not want a supplementary view in a particular case, your layout object should not create the attributes for that view. Alternatively, you can hide views by setting the hidden property of the corresponding attributes to YEStrue or set the alpha property of the attributes to 0. To hide header and footer views in a flow layout, you can also set the width and height of those views to 0.

    Availability

    Available in iOS 6.0 and later.

  • Asks your data source object whether the specified item can be moved to another location in the collection view.

    Declaration

    Swift

    optional func collectionView(_ collectionView: UICollectionView, canMoveItemAtIndexPath indexPath: NSIndexPath) -> Bool

    Objective-C

    - (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath

    Parameters

    collectionView

    The collection view requesting this information.

    indexPath

    The index path of the item that the collection view is trying to move.

    Return Value

    YEStrue if the item is allowed to move or NOfalse if it is not.

    Discussion

    Use this method to selectively allow or disallow the movement of items within a collection view. If you do not implement this method, but you do implement the collectionView:moveItemAtIndexPath:toIndexPath: method, the collection view allows all items to be reordered.

    Availability

    Available in iOS 9.0 and later.

  • Tells your data source object to move the specified item to its new location.

    Declaration

    Swift

    optional func collectionView(_ collectionView: UICollectionView, moveItemAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath)

    Objective-C

    - (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath

    Parameters

    collectionView

    The collection view notifying you of the move.

    sourceIndexPath

    The item’s original index path.

    destinationIndexPath

    The new index path of the item.

    Discussion

    You must implement this method to support the reordering of items within the collection view. If you do not implement this method, the collection view ignores any attempts to reorder items.

    When interactions with an item end, the collection view calls this method if the position of the item changed. Use this method to update your data structures with the new index path information.

    Availability

    Available in iOS 9.0 and later.