Protocol: UICollectionViewDataSource

Overview

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.Asks the data source for the cell that corresponds to the specified item in the collection view. (required)Asks the data source for the number of items in the specified section. (required)Asks the collection view to provide a supplementary view to display in the collection view.Asks the data source for the number of sections in the collection view.

Instance Method Summary (collapse)

Instance Method Details

- (UICollectionViewCell) collectionView(collectionView, cellForItemAtIndexPath:indexPath)

Asks the data source for the cell that corresponds to the specified item in the collection view. (required) 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 placement of cells is handled automatically by the layout object in conjunction with the collection view.

Parameters:

  • collectionView (UICollectionView)

    An object representing the collection view requesting this information.

  • indexPath (NSIndexPath)

    The index path that specifies the location of the item.

Returns:

- (Integer) collectionView(collectionView, numberOfItemsInSection:section)

Asks the data source for the number of items in the specified section. (required)

Parameters:

  • collectionView (UICollectionView)

    An object representing the collection view requesting this information.

  • section (Integer)

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

Returns:

  • (Integer)

    The number of rows in section.

- (UICollectionReusableView) collectionView(collectionView, viewForSupplementaryElementOfKind:kind, atIndexPath:indexPath)

Asks the collection view to provide a supplementary view to display in the collection view. 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 placement of supplementary views is handled automatically by the layout object in conjunction with the collection view.

Parameters:

  • collectionView (UICollectionView)

    An object representing the collection view requesting this information.

  • kind (String)

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

  • indexPath (NSIndexPath)

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

Returns:

- (Integer) numberOfSectionsInCollectionView(collectionView)

Asks the data source for the number of sections in the collection view. If you do not implement this method, the collection view uses a default value of 1.

Parameters:

  • collectionView (UICollectionView)

    An object representing the collection view requesting this information.

Returns:

  • (Integer)

    The number of sections in collectionView.