import UIKit
import RxSwift
import RxCocoa
import RxDataSources
import Differentiator
struct MySection {
var header: String
var items: [Item]
}
extension MySection : AnimatableSectionModelType {
typealias Item = Int
var identity: String {
return header
}
init(original: MySection, items: [Item]) {
self = original
self.items = items
}
}
class UICollectionViewSimpleViewController: UIViewController {
@IBOutlet weak var collectionView: UICollectionView!
let disposeBag = DisposeBag()
var dataSource: RxCollectionViewSectionedAnimatedDataSource<MySection>?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let sections = [
MySection(header: "First section", items: [
1,
2
]),
MySection(header: "Second section", items: [
3,
4
])
]
setDataSource(sections: sections)
}
}
// MARK:- UICollectionView
extension UICollectionViewSimpleViewController: UICollectionViewDelegate{
}
extension UICollectionViewSimpleViewController{
static func collectionViewDataSourceUI() -> (
CollectionViewSectionedDataSource<MySection>.ConfigureCell,
CollectionViewSectionedDataSource<MySection>.ConfigureSupplementaryView
) {
return (
{ (_, cv, ip, i) in
let cell = cv.dequeueReusableCell(withReuseIdentifier: "Cell", for: ip)
return cell
},
{ (ds ,cv, kind, ip) in
let section = cv.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "Section", for: ip)
return section
}
)
}
fileprivate func setDataSource(sections: [MySection]) {
let (configureCollectionViewCell, configureSupplementaryView) = UICollectionViewSimpleViewController.collectionViewDataSourceUI()
let cvAnimatedDataSource = RxCollectionViewSectionedAnimatedDataSource(
configureCell: configureCollectionViewCell,
configureSupplementaryView: configureSupplementaryView
)
self.dataSource = cvAnimatedDataSource
Observable.just(sections)
.bind(to: collectionView.rx.items(dataSource: cvAnimatedDataSource))
.disposed(by: disposeBag)
collectionView.rx.setDelegate(self)
.disposed(by: disposeBag)
}
}
Simulator Screen Shot - iPhone 8 Plus - 2018-01-02 at 21.48.03.png