EpoxyCollectionView provides a declarative API for driving UICollectionView content. You can use CollectionViewController, a subclassable UIViewController, to manage a collection view.
For simple use cases, you can instantiate CollectionViewController directly with a layout and an items closure. For more complex state management, subclass CollectionViewController and use the @ItemModelBuilder attribute on a property that returns [ItemModeling]. When the state changes, call setItems(items, animated: true) to update the collection view.
// Simple instantiation
enum DataID { case row }
let viewController = CollectionViewController(
layout: UICollectionViewCompositionalLayout.list(using: .init(appearance: .plain)),
items: {
TextRow.itemModel(
dataID: DataID.row,
content: .init(title: "Tap me!"),
style: .small)
.didSelect { _ in
// Handle selection
}
})
// Subclassing for stateful content
class CounterViewController: CollectionViewController {
init() {
let layout = UICollectionViewCompositionalLayout.list(using: .init(appearance: .plain))
super.init(layout: layout)
setItems(items, animated: false)
}
enum DataID { case row }
var count = 0 {
didSet {
setItems(items, animated: true)
}
}
@ItemModelBuilder
var items: [ItemModeling] {
TextRow.itemModel(
dataID: DataID.row,
content: .init(
title: "Count \(count)",
body: "Tap to increment"),
style: .large)
.didSelect { [weak self] _ in
self?.count += 1
}
}
}