UITableView vs UICollectionView

The interviewer asks

“When would you use UITableView vs UICollectionView?”

How to answer

For new UIKit screens, the default is UICollectionView for both lists and grids: a list is just a collection view with a list layout (UICollectionLayoutListConfiguration), and it gets the same table-style cells, swipe actions, accessories and sidebar styles. Collection views also handle multi-column grids, carousels and mixed sections that adapt to the available width, which matters now that iPhone apps resize (iPhone Mirroring, iPad, iPhone Duo). UITableView isn't deprecated and is still fine for simple single-column screens and the large amount of existing code built on it, so you should know both.

Key points

  • Collection view + list configuration: .plain, .grouped, .insetGrouped, .sidebar, .sidebarPlain appearances; UICollectionViewListCell with accessories; swipe actions via trailingSwipeActionsConfigurationProvider.
  • Collection view extras: compositional layout (grids, orthogonal carousels, per-section layouts that react to container width), CellRegistration, outline/expandable sections via section snapshots.
  • Table view: single column only, built-in .plain / .grouped / .insetGrouped styles, simple API, mature editing/reordering support.
  • Shared: cell reuse, diffable data sources, prefetching, content configurations (UIListContentConfiguration), UIHostingConfiguration.
  • SwiftUI's List is the third option; you can also host SwiftUI content in collection view cells.

If they dig deeper

  • Why move existing table views? Usually you don't need to; move when you need a grid, a sidebar, mixed layouts, or width-adaptive columns.
  • Adaptive layouts: a single collection view can be a list at narrow widths and a grid at wide widths by switching section layouts on environment.container.effectiveContentSize.width — no idiom or device checks.
  • Self-sizing: both support it; collection views use .estimated dimensions, tables use UITableView.automaticDimension.
  • Performance: equivalent for typical lists; both rely on reuse, so configuration cost per cell matters more than the container.

Common pitfalls

  • Choosing UITableView for a new screen and later bolting on a grid or sidebar.
  • Hard-coding item sizes for "the" iPhone width; derive them from the container.
  • Mixing reloadData with diffable updates.
  • Assuming a UITableViewController gives you anything a list-configured collection view doesn't (only clearsSelectionOnViewWillAppear / refresh-control convenience).

How to use it

swift
import UIKit

@MainActor
func makeAdaptiveLayout() -> UICollectionViewCompositionalLayout {
    UICollectionViewCompositionalLayout { _, environment in
        let width = environment.container.effectiveContentSize.width
        if width < 500 {                                    // narrow: a table-style list
            let config = UICollectionLayoutListConfiguration(appearance: .insetGrouped)
            return NSCollectionLayoutSection.list(using: config, layoutEnvironment: environment)
        }
        let columns = Int(width / 220)                      // wide: a grid
        let item = NSCollectionLayoutItem(layoutSize: .init(
            widthDimension: .fractionalWidth(1 / CGFloat(columns)), heightDimension: .estimated(160)))
        let group = NSCollectionLayoutGroup.horizontal(layoutSize: .init(
            widthDimension: .fractionalWidth(1), heightDimension: .estimated(160)), subitems: [item])
        return NSCollectionLayoutSection(group: group)
    }
}

// Classic table view, still common in existing code
@MainActor
func makeTable() -> UITableView {
    let tableView = UITableView(frame: .zero, style: .insetGrouped)
    tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
    return tableView
}

Best practices

  • Default to UICollectionView with a list or compositional layout for new UIKit screens.
  • Keep existing UITableView code unless you need what collection views add.
  • Use diffable data sources and content configurations with either.
  • Derive layout from the container width; test with the resizable simulator in Device Hub and the iPhone Duo simulator.
  • Consider SwiftUI List for new screens in SwiftUI-first apps.

Related concepts

  • UICollectionView list configuration, Compositional layout, Cell reuse, Diffable data source, Prefetching, SwiftUI List