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. More...

Inheritance


Not Applicable

Import Statement


import UIKit @import UIKit;

Availability


Available in iOS 6.0 and later.
  • Asks the data source for the number of items in the specified section. (required)

    Declaration

    Swift

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

    Objective-C

    - (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.

    Import Statement

    import UIKit

    Availability

    Available in iOS 6.0 and later.

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

    Declaration

    Swift

    optional func numberOfSectionsInCollectionView(_ collectionView: UICollectionView) -> Int

    Objective-C

    - (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.

    Import Statement

    import UIKit

    Availability

    Available in iOS 6.0 and later.

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

    Declaration

    Swift

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

    Objective-C

    - (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.

    Import Statement

    import UIKit

    Availability

    Available in iOS 6.0 and later.

  • Asks the collection view 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

    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 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.

    Import Statement

    import UIKit

    Availability

    Available in iOS 6.0 and later.