UICollectionView vs UITableView and diffable data sources

UIKit Mid-level Swift 5.0+ iOS 17+ Reviewed By Alok Singh

Pick the fix

You need a grid on iPad, a list on iPhone, and animated updates without crashes from mismatched counts. Which approach?

Options
Hint 1 of 1

Which option separates data from layout?

Choose an answer, then open Show answer below to check it.

Show answer

Answer: A UICollectionView with a compositional layout and a UICollectionViewDiffableDataSource

Correct. One view adapts its layout, and snapshots animate changes safely.

Why not the others?
  • B UITableView with reloadData(). No grid, and no animations.
  • C performBatchUpdates with hand-computed index paths. That is exactly where 'invalid number of items' crashes come from.
  • D UIStackViews inside a UIScrollView. No cell reuse, so long lists use too much memory.

30-second answer

UITableView is a single-column list. UICollectionView separates data from layout, so the same data can be a list, a grid or anything custom, and with list configurations it covers table views too. Diffable data sources apply snapshots of identifiers and animate the difference, which removes 'invalid number of items' crashes from manual batch updates.

Practise saying it · 45 s. No recording, just a timer

Compare collection and table views, and explain diffable data sources.

Say your answer out loud, then check which of these points you covered:

  • Layout separated from data
  • Snapshots of identifiers
  • No count-mismatch crashes

Deep dive

Identifiers must be stable and unique; use ids, not whole model values.

reconfigureItems (iOS 15) updates cells without recreating them.

Apply snapshots on one queue consistently, usually the main one.

UICollectionLayoutListConfiguration gives table-style lists in a collection view.

Common mistakes

  • Using model structs with changing fields as identifiers.
  • Manual insertRows calls that disagree with the data.

Senior take & follow-ups

Seniors default to collection views with diffable data sources and explain identifier design.

Follow-up questions

Think of your answer first, then open the hint.

  1. What is reconfigure vs reload?

    Show hint

    Reconfigure reuses the existing cell; reload replaces it.

  2. Why use ids as items?

    Show hint

    Content changes shouldn't look like delete + insert.

From the author

Catalog - UI

Made by the author: browse UIKit components with live demos, and copy their code snippets into your project.

Suggest a fix if something here is wrong or out of date.