Skip to content

Instantly share code, notes, and snippets.

@steve21124
Created August 25, 2017 05:07
Show Gist options
  • Save steve21124/2de1d70850014075dc518d5b3af5835b to your computer and use it in GitHub Desktop.
Save steve21124/2de1d70850014075dc518d5b3af5835b to your computer and use it in GitHub Desktop.
import UIKit
import DataSources
protocol Model {
var title: String { get }
}
struct ModelM : Model, Diffable {
var diffIdentifier: AnyHashable {
return AnyHashable(identity)
}
let identity: String
let title: String
}
final class Cell : UICollectionViewCell{
let label = UILabel()
private let paddingView = UIView()
override init(frame: CGRect) {
super.init(frame: frame)
contentView.addSubview(paddingView)
contentView.addSubview(label)
label.textAlignment = .center
label.font = UIFont.systemFont(ofSize: 20)
paddingView.backgroundColor = UIColor(white: 0.95, alpha: 1)
paddingView.layer.cornerRadius = 4
paddingView.layer.shouldRasterize = true
paddingView.layer.rasterizationScale = UIScreen.main.scale
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
let collectionView = UICollectionView()
private lazy var sectionDataController = SectionDataController<ModelM, CollectionViewAdapter>(
adapter: CollectionViewAdapter(collectionView: self.collectionView),
isEqual: { $0.identity == $1.identity } // If Model has Equatable, you can omit this closure.
)
var models: [ModelM] = [] {
didSet {
sectionDataController.update(items: models, updateMode: .partial(animated: true), completion: {
// Completed update
})
}
}
private lazy var dataSource = CollectionViewDataSource(sectionDataSource: sectionDataController)
override func viewDidLoad() {
super.viewDidLoad()
collectionView.delegate = self
collectionView.dataSource = self
self.view.addSubview(collectionView)
collectionView.frame = self.view.bounds
collectionView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
dataSource.cellFactory = { _, collectionView, indexPath, model in
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! Cell
cell.label.text = model.title
return cell
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return dataSource.numberOfSections(in:)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return dataSource.numberOfItems(in: section)
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! Cell
let m = dataSource.item(at: indexPath, in: section0)
cell.label.text = m.title
return cell
}
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
if kind == UICollectionElementKindSectionHeader {
let view = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "Header", for: indexPath) as! Header
view.label.text = "Section " + indexPath.section.description
return view
}
fatalError()
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSize(width: collectionView.bounds.width, height: 50)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.bounds.width / 6, height: 50)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment