Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- open class CollectionDataSource<Provider: CollectionDataProviderProtocol, Cell: UICollectionViewCell>: NSObject, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout where Cell: ConfigurableCell, Provider.T == Cell.T {
- // MARK: - Private Properties
- let provider: Provider
- let collectionView: UICollectionView
- // Initialize and setup CollectionDataSource
- // CollectionDataSource sets itself as the delegate for UICollectionViewDataSource and UICollectionViewDelegate
- init(collectionView: UICollectionView, provider: Provider) {
- self.collectionView = collectionView
- self.provider = provider
- super.init()
- setup()
- }
- func setup(){
- collectionView.dataSource = self
- collectionView.delegate = self
- }
- public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
- return provider.numberOfItems(in: section)
- }
- public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
- return CGSize(width: collectionView.frame.width / 2 - 20, height: 250)
- }
- public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
- guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: Cell.reuseIdentifier, for: indexPath) as? Cell else {
- return UICollectionViewCell()
- }
- let item = provider.item(at: indexPath)
- if let item = item {
- cell.configure(item, at: indexPath)
- }
- return cell
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement