CollectionViewDriver

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

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

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

    Declaration

    Swift

    @MainActor
    public let options: CollectionViewDriverOptions
  • The collection view model.

    Declaration

    Swift

    @Published
    @MainActor
    public private(set) var viewModel: CollectionViewModel { get set }
  • A scroll view delegate object to receive forwarded events.

    Declaration

    Swift

    @MainActor
    public weak var scrollViewDelegate: UIScrollViewDelegate?
  • A flow layout delegate object to receive forwarded events.

    Declaration

    Swift

    @MainActor
    public weak var flowLayoutDelegate: UICollectionViewDelegateFlowLayout?

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

    @MainActor
    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.

State Information

  • The number of sections displayed by the collection view.

    Declaration

    Swift

    @MainActor
    public var numberOfSections: Int { get }
  • Returns the count of items in the specified section.

    Declaration

    Swift

    @MainActor
    public func numberOfItems(in section: Int) -> Int

    Parameters

    section

    The index of the section for which you want a count of the items.

    Return Value

    The number of items in the specified section.

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

    @MainActor
    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.

  • An async version of update(viewModel:animated:completion:).

    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

    @MainActor
    public func update(viewModel new: CollectionViewModel, animated: Bool = true) async

    Parameters

    viewModel

    The new collection view model.

    animated

    Whether or not to animate updates.