7.5 · Compositional layout, diffable data sources, cell reuse and prefetching

Collection views are the workhorse of modern UIKit, and, with list layouts, now replace most table views. Four ideas cover them: a declarative layout, an identity-based data source, reusable cells and cheap scrolling.

Mental model. Describe the layout as item, group and section. Describe the data as a snapshot of identifiers. Let UIKit work out the difference and reuse cells to show it.

Layout and data

Compositional layout and the diffable data source.

  1. How does compositional layout describe a collection view, and how do you read its output? 7.5.1 · L3
    UICollectionViewCompositionalLayout builds a layout from three nested pieces. An item (NSCollectionLayoutItem) describes one cell’s size. A group arranges…
  2. How does a diffable data source work, and what does it require of your item identifiers? 7.5.2 · L3
    A UICollectionViewDiffableDataSource<Section, Item> replaces the old “answer numberOfItems and hope” protocol. You build an immutable snapshot…

Cells and performance

Reuse bugs, prefetching and self-sizing.

  1. How does cell reuse work, and what are the classic reuse bugs? 7.5.3 · L3
    Collection and table views do not create a cell per item. They keep a small pool and reuse cells as they scroll off screen: you ask the view to…
  2. How do prefetching and self-sizing cells keep scrolling smooth, and what can still make it janky? 7.5.4 · L5
    Prefetching is on by default (isPrefetchingEnabled is true): the collection view asks for cells slightly before they are needed, spreading creation cost across…

More in UIKit