-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollectionViewDataSource.swift
More file actions
80 lines (64 loc) · 2.45 KB
/
CollectionViewDataSource.swift
File metadata and controls
80 lines (64 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//
// CollectionViewDataSource.swift
// CollectionViewDataSource
//
// Created by Havelio Henar on 09/09/20.
// Copyright © 2020 Havelio Henar. All rights reserved.
//
import UIKit
/**
CollectionViewDataSource is a class for handling general dataSource of UiCollectionView,
So we don't need exten UICollectionViewDataSource to each controller
how to use:
class UserCell: BaseCollectionViewCell<User> {
override var item: User! {
didSet {
// hanlde your data here
}
}
}
// define the generic model type:
var dataSource: CollectionViewDataSource<UserCell, User>!
let users = [User]()
dataSource = .make(items: users, collection: collectionView)
// for reload collectionView:
dataSource.reload(data: users)
// for custom configuration:
dataSource = .make(items: users, collection: collectionView) { [weak self] cell, indexPath in
// handle your custom code here
}
*/
class CollectionViewDataSource<Cell: BaseCollectionViewCell<Model>, Model>: NSObject, UICollectionViewDataSource {
typealias PrepareCell = (Cell, IndexPath) -> Void
private var prepareCell: PrepareCell? = nil
private var collection: UICollectionView? = nil
var items: [Model]! {
didSet { collection?.reloadData() }
}
static func make(items: [Model], collection: UICollectionView,
prepareCell: PrepareCell? = nil) -> CollectionViewDataSource {
let colelctionDataSource = CollectionViewDataSource()
colelctionDataSource.collection = collection
colelctionDataSource.items = items
colelctionDataSource.prepareCell = prepareCell
collection.dataSource = colelctionDataSource
return colelctionDataSource
}
func reload(data: [Model]) {
self.items = data
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return items.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: Cell.identifier,
for: indexPath) as! Cell
cell.item = items[indexPath.row]
prepareCell?(cell, indexPath)
return cell
}
}
class BaseCollectionViewCell<Model>: UICollectionViewCell {
class var identifier: String { return String(describing: self) }
var item: Model!
}