UICollectionViewDataSource Protocol Reference

Conforms to
Framework
/System/Library/Frameworks/UIKit.framework
Availability
Available in iOS 6.0 and later.
Declared in
UICollectionView.h

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.

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.

Tasks

Getting Item and Section Metrics

Getting Views for Items

Instance Methods

collectionView:cellForItemAtIndexPath:

Asks the data source for the cell that corresponds to the specified item in the collection view. (required)

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

An object representing 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.
Declared In
UICollectionView.h

collectionView:numberOfItemsInSection:

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

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
Parameters
collectionView

An object representing 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 rows in section.

Availability
  • Available in iOS 6.0 and later.
Declared In
UICollectionView.h

collectionView:viewForSupplementaryElementOfKind:atIndexPath:

Asks the collection view to provide a supplementary view to display in the collection view.

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

An object representing 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 YES 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.
Declared In
UICollectionView.h

numberOfSectionsInCollectionView:

Asks the data source for the number of sections in the collection view.

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
Parameters
collectionView

An object representing the collection view requesting this information.

Return Value

The number of sections in collectionView.

Discussion

If you do not implement this method, the collection view uses a default value of 1.

Availability
  • Available in iOS 6.0 and later.
Declared In
UICollectionView.h