Framework to manage complex UICollectionView in declarative way and very few lines of code.
Heavily inspired by https://github.com/maxsokolov/TableKit and https://github.com/Instagram/IGListKit
Development still in progress. Some changes may affect backward compatibility
- Declarative
UICollectionViewmanagement - No need to implement
UICollectionViewDataSourceandUICollectionViewDelegate - Easy way to map your models into cells
- Auto diffing
- Supports cells & reusable views from code and xibs and storyboard
- Flexible
- Register cells and reusable views automatically
- Fix scroll indicator clipping at iOS11 (http://www.openradar.me/34308893)
Key concepts of CollectionKit are Section, Item and Director.
Item is responsible for UICollectionViewCell configuration, size and actions
Section is responsible for group of items and provides information for each item, header/footer and all kind of insets/margins: section insets, minimum inter item spacing and line spacing
Director is responsible for providing all information, needed for UICollectionView and its updations
Setup UICollectionView and director:
Setup collection view
collectionView = UICollectionView(frame: view.bounds, colletionViewLayout: UICollectionViewFlowLayout())
collectionDirector = CollectionDirector(colletionView: collectionView)Create items
let item1 = CollectionItem<CollectionCell>(item: "hello!")
let item2 = CollectionItem<CollectionCell>(item: "im")
let item3 = CollectionItem<CollectionCell>(item: "ColletionKit")
let item4 = CollectionItem<ImageCell>(item: "greeting.png")Create section and put items in section
let section = CollectionSection()
let items = [item1, item2, item3, item4]
section += items
director += sectionPut section in director and reload director
director += section
director.reload()Cell must implement ConfigurableCollectionCell protocol. You need to specify cell size and configure methods:
extension CollectionCell : ConfigurableCollectionItem {
static func estimatedSize(item: String?, boundingSize: CGSize) -> CGSize {
return CGSize(width: collectionViewSize.width, height: 50)
}
func configure(item: String) {
textLabel.text = item
}
}Implement such actions like didSelectItem or shouldHighlightItem using functional syntax
let row = CollectionItem<CollectionCell>(item: "text")
.onSelect({ (_) in
print("i was tapped!")
}).onDisplay({ (_) in
print("i was displayed")
})Available actions:
onSelectonDeselectonDisplayonEndDisplayonHighlightonUnighlight
You can setup inter item spacing, line spacing and section insets using section object:
let section = CollectionSection()
section.minimumInterItemSpacing = 2
section.insetForSection = UIEdgeInsetsMake(0, 20, 0, 20)
section.lineSpacing = 2You can update collection view only by operating with section and item objects without any IndexPath mess: put usual operations in performUpdates block
self.director.performUpdates { [unowned self] in
self.section.append(item: row)
self.section.insert(item: row, at: 0)
self.section.remove(item: item)
}You can provide your own section implementations using AbstractCollectionSection protocol. For example, you can use it for using CollectionDirector with Realm.Results and save Results lazy behaviour or implementing expandable sections