Contents

UICollectionView.CellRegistration

A registration for the collection view’s cells.

Declaration

struct CellRegistration<Cell, Item> where Cell : UICollectionViewCell

Overview

Use a cell registration to register cells with your collection view and configure each cell for display. You create a cell registration with your cell type and data item type as the registration’s generic parameters, passing in a registration handler to configure the cell. In the registration handler, you specify how to configure the content and appearance of that type of cell.

The following example creates a cell registration for cells of type UICollectionViewListCell. It creates a content configuration with a system default style, customizes the content and appearance of the configuration, and then assigns the configuration to the cell.

let cellRegistration = UICollectionView.CellRegistration<UICollectionViewListCell, Int> { cell, indexPath, item in
    
    var contentConfiguration = cell.defaultContentConfiguration()
    
    contentConfiguration.text = "\(item)"
    contentConfiguration.textProperties.color = .lightGray
    
    cell.contentConfiguration = contentConfiguration
}

After you create a cell registration, you pass it in to dequeueConfiguredReusableCell(using:for:item:), which you call from your data source’s cell provider.

dataSource = UICollectionViewDiffableDataSource<Section, Int>(collectionView: collectionView) {
    (collectionView: UICollectionView, indexPath: IndexPath, itemIdentifier: Int) -> UICollectionViewCell? in
    
    return collectionView.dequeueConfiguredReusableCell(using: cellRegistration,
                                                        for: indexPath,
                                                        item: itemIdentifier)
}

You don’t need to call register(_:forCellWithReuseIdentifier:) or register(_:forCellWithReuseIdentifier:). The collection view registers your cell automatically when you pass the cell registration to dequeueConfiguredReusableCell(using:for:item:).

Topics

Creating a cell registration

See Also

Creating cells