CollectionViewDriver

@MainActor
public final class CollectionViewDriver : NSObject
extension CollectionViewDriver: UICollectionViewDelegate

Represents the main entry-point to the library, and underlying UICollectionView components. A CollectionViewDriver is responsible for “driving” the collection view. It handles all layout, data source, delegate, and diffing operations.

  • A closure type used to notify callers of collection view updates.

    Declaration

    Swift

    public typealias DidUpdate = @MainActor (CollectionViewDriver) -> Void
  • The collection view.

    Declaration

    Swift

    public let view: UICollectionView
  • A set of options to customize behavior of the driver.

    Declaration

    Swift

    public let options: CollectionViewDriverOptions
  • The collection view model.

    Declaration

    Swift

    @Published
    public private(set) var viewModel: CollectionViewModel { get set }

Init

  • Initializes a new CollectionViewDriver.

    Warning

    The driver does not retain the cellEventCoordinator, because this object is typically the view controller that owns the driver. Thus, the caller is responsible for retaining and keeping alive the cellEventCoordinator for the entire lifetime of the driver.

    Declaration

    Swift

    public init(view: UICollectionView,
                viewModel: CollectionViewModel = .empty,
                options: CollectionViewDriverOptions = .init(),
                emptyViewProvider: EmptyViewProvider? = nil,
                cellEventCoordinator: CellEventCoordinator? = nil)

    Parameters

    view

    The collection view.

    viewModel

    The collection view model.

    options

    A set of options to customize behavior of the driver.

    emptyViewProvider

    An empty view provider.

    cellEventCoordinator

    The cell event coordinator, if you wish to handle cell events outside of your cell view models. Note: This object is not retained by the driver.

Applying Updates

  • Updates the collection with the provided viewModel. This method will trigger a diff between the previous view model and the newly provided view model.

    Warning

    If you provide a viewModel with an id different from the previous one, this is considered a replacement. By default, the driver will animate the diff between the view models. You can customize this behavior via the options for the driver.

    Declaration

    Swift

    public func update(viewModel new: CollectionViewModel, animated: Bool = true, completion: DidUpdate? = nil)

    Parameters

    viewModel

    The new collection view model.

    animated

    Whether or not to animate updates.

    completion

    A closure to call when the driver finishes diffing and updating the collection view. The driver passes itself to the closure. It is always called on the main thread.