DTCollectionViewManager
Features
- Powerful mapping system between data models and cells, headers and footers
- Automatic datasource and interface synchronization.
- Flexible Memory/CoreData/Realm/diffable datasource storage options
- Powerful compile-time safe events system, that covers all of UICollectionView delegate methods
- Views created from code, XIB, or storyboard, automatic registration and dequeue
- Can be used with UICollectionViewController, or UIViewController with UICollectionView
- Built-in support for iOS 14 UICollectionView.CellRegistration and content configuration
- Unified syntax with DTTableViewManager
- Complete documentation
- API Reference
Read more about latest 11.0 release and integration with SwiftUI!
Requirements
- Xcode 12+
- iOS 11.0+ / tvOS 11.0+ / macCatalyst 13.0+
- Swift 5.3+
If you need Xcode 11 support or Swift 4...Swift 5.2, or iOS 8...iOS 10 support, you can use 7.x releases.
Installation
Swift Package Manager
Add package into Xcode Project settings -> Swift Packages
CocoaPods:
pod 'DTCollectionViewManager', '~> 11.0.0-beta.1'
Quick start
Let's say you have an array of Posts you want to display in UICollectionView. To quickly show them using DTCollectionViewManager, here's what you need to do:
- Create UICollectionViewCell subclass, let's say PostCell and adopt
ModelTransfer
protocol:
class PostCell : UICollectionViewCell, ModelTransfer {
func update(with model: Post) {
// Fill your cell with actual data
}
}
- In your view controller:
class PostsViewController: UICollectionViewController, DTCollectionViewManageable {
override func viewDidLoad() {
super.viewDidLoad()
// Register PostCell to be used with this controller's collection view
manager.register(PostCell.self)
// Populate datasource
manager.memoryStorage.setItems(posts)
}
}
Make sure your UICollectionView outlet is wired to your class (or use UICollectionViewController subclass). If you have a PostCell.xib file, it will be automatically used for dequeueing PostCell.
- That's it! It's that easy!
Of course, cool stuff does not stop there, framework supports all datasource and delegate methods as closures, conditional mappings and much much more! Choose what interests you in the next section of readme.
Burning questions
Starter pack
- Why do I need this library?
- How data models are mapped to cells?
- Can I use unsubclassed UICollectionViewCell or UICollectionReusableView (for example UICollectionViewListCell)?
- How can I register views to dequeue from code/xib/storyboard?
- How can I use the same cells differently in different places?
- What datasource options do I have?(e.g. memory/CoreData/Realm/diffable datasources)
- How can I implement datasource/delegate methods from
UICollectionView
?
Advanced
- Can I implement delegate methods instead of using DTCollectionViewManager event closures?
- How can I react to and customize UICollectionView updates?
- What if something goes wrong?
Sample code and documentation
Thanks
- Alexey Belkevich for providing initial implementation of CellFactory.
- Michael Fey for providing insight into NSFetchedResultsController updates done right.
- Nickolay Sheika for great feedback, that helped shaping 3.0 release and future direction of the library.
- Artem Antihevich for great discussions about Swift generics and type capturing.