Contents

cellForItem(at:)

Gets the cell object at the index path you specify.

Declaration

func cellForItem(at indexPath: IndexPath) -> UICollectionViewCell?

Parameters

  • indexPath:

    The index path that specifies the section and item number of the cell.

Return Value

The cell object at the corresponding index path. In versions of iOS earlier than iOS 15, this method returns nil if the cell isn’t visible or if indexPath is out of range. In iOS 15 and later, this method returns a non-nil cell if the collection view retains a prepared cell at the specified index path, even if the cell isn’t currently visible.

Discussion

In iOS 15 and later, the collection view retains a prepared cell in the following situations:

  • Cells that the collection view prefetches and retains in its cache of prepared cells, but that aren’t visible because the collection view hasn’t displayed them yet.

  • Cells that the collection view finishes displaying and continues to retain in its cache of prepared cells because they remain near the visible region and might scroll back into view.

  • The cell that contains the first responder.

  • The cell that has focus.

See Also

Locating items and views in the collection view