/
CollectionView.swift
301 lines (237 loc) · 10.8 KB
/
CollectionView.swift
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
//
// CollectionView.swift
// CollectionView-simplify-business
//
// Created by 孙继刚 on 2017/10/12.
// Copyright © 2017年 孙继刚. All rights reserved.
//
// # 很贴心的预留了前缀`KTJ`,方便全文件替换 在实际使用中,可以去除下面的别名
typealias ListItemDefaultProtocol = KTJListItemDefaultProtocol
typealias CollectionViewSection = KTJCollectionViewSection
typealias CollectionView = KTJCollectionView
import UIKit
import CHTCollectionViewWaterfallLayout
protocol KTJListItemProtocol {
var identifa: String { get }
var newItem: AnyObject { get }
var registClass: AnyClass { get }
func fillModel(item: AnyObject)
}
protocol KTJListItemDefaultProtocol: KTJListItemProtocol {
associatedtype ItemType: UIView
func fillModel(view: ItemType)
}
extension KTJListItemDefaultProtocol {
var identifa: String { return NSStringFromClass(ItemType.self)}
var newItem: AnyObject { return ItemType() }
var registClass: AnyClass { return ItemType.self }
func fillModel(item: AnyObject) {
if let reusableView = item as? ItemType {
fillModel(view: reusableView)
}
}
}
class KTJCollectionViewSection: NSObject {
/// section的唯一标识符,此处交给数据初始化设置,默认为""<==>无
var identifier = ""
/// 该section上左下右的边距
var sectionInset = UIEdgeInsets.zero
/// 列数,默认为1
var columnCount: Int?
/// 该section列的最小距离
var minimumColumnSpacing = 0.0
/// 该swction行的最小距离
var minimumInteritemSpacing = 0.0
var header: KTJListItemProtocol?
var items: [KTJListItemProtocol]?
var footer: KTJListItemProtocol?
}
/// 只有部分可用。具体更新参看 class KTJCollectionView
protocol KTJCollectionViewDelegate: NSObjectProtocol {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
}
extension KTJCollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { }
}
/// 携带格式化好数据源瀑布流
class KTJCollectionView: UICollectionView, UICollectionViewDataSource, UICollectionViewDelegate, CHTCollectionViewDelegateWaterfallLayout {
weak var allDelegate: KTJCollectionViewDelegate?
/// 数据源
var sections = [KTJCollectionViewSection]() {
didSet {
for section in sections {
if let header = section.header {
self.register(header.registClass,
forSupplementaryViewOfKind: CHTCollectionElementKindSectionHeader,
withReuseIdentifier: header.identifa)
}
if let footer = section.footer {
self.register(footer.registClass,
forSupplementaryViewOfKind: CHTCollectionElementKindSectionFooter,
withReuseIdentifier: footer.identifa)
}
if let items = section.items {
for item in items {
self.register(item.registClass, forCellWithReuseIdentifier: item.identifa)
}
}
}
self.reloadData()
}
}
private var viewCache = NSCache<AnyObject, AnyObject>()
convenience init() {
let layout = CHTCollectionViewWaterfallLayout()
layout.sectionInset = UIEdgeInsets.zero
layout.minimumColumnSpacing = 0
layout.minimumInteritemSpacing = 0
self.init(frame: CGRect.zero, collectionViewLayout: layout)
}
override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
super.init(frame: frame, collectionViewLayout: layout)
setup()
}
required convenience init?(coder aDecoder: NSCoder) {
self.init()
setup()
}
/// 基础配置
private func setup() {
self.delegate = self
self.dataSource = self
}
// MARK: - UICollectionViewDataSource
func numberOfSections(in collectionView: UICollectionView) -> Int {
return sections.count
}
func collectionView(_ collectionView: UICollectionView!, layout collectionViewLayout: UICollectionViewLayout!, columnCountForSection section: Int) -> Int {
var columnCount = 0
if section < sections.count {
columnCount = sections[section].columnCount ?? 1
}
return columnCount
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
var items = 0
if section < sections.count {
items = sections[section].items?.count ?? 0
}
return items
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
var returnCell: UICollectionViewCell?
if indexPath.section < sections.count,
indexPath.row < (sections[indexPath.section].items?.count ?? 0),
let item = sections[indexPath.section].items?[indexPath.row] {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: item.identifa,
for: indexPath)
item.fillModel(item: cell)
cell.sizeToFit()
returnCell = cell
}
return returnCell ?? UICollectionViewCell.init()
}
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
var returnView: UICollectionReusableView?
var item: KTJListItemProtocol?
if indexPath.section < sections.count {
if kind == CHTCollectionElementKindSectionHeader {
item = sections[indexPath.section].header
} else if kind == CHTCollectionElementKindSectionFooter {
item = sections[indexPath.section].footer
}
}
if let item = item {
let view = collectionView.dequeueReusableSupplementaryView(ofKind: kind,
withReuseIdentifier: item.identifa,
for: indexPath)
item.fillModel(item: view)
returnView = view
}
return returnView ?? UICollectionReusableView.init()
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
self.allDelegate?.collectionView(self, didSelectItemAt: indexPath)
}
// MARK: - CHTCollectionViewDelegateWaterfallLayout
func collectionView(_ collectionView: UICollectionView!, layout collectionViewLayout: UICollectionViewLayout!, sizeForItemAt indexPath: IndexPath!) -> CGSize {
var returnSize: CGSize?
if indexPath.section < sections.count,
indexPath.row < (sections[indexPath.section].items?.count ?? 0),
let item = sections[indexPath.section].items?[indexPath.row] {
if let cell = (viewCache.object(forKey: item.identifa as AnyObject) ?? item.newItem)
as? UICollectionViewCell {
let section = sections[indexPath.section]
if let columnCount = section.columnCount,
columnCount > 1 {
var width = collectionView.bounds.width
- section.sectionInset.left
- section.sectionInset.right
- CGFloat(columnCount-1) * CGFloat(section.minimumColumnSpacing)
width /= CGFloat(columnCount)
cell.frame = CGRect(x: 0, y: 0, width: width, height: collectionView.bounds.height)
} else {
cell.frame = CGRect(x: 0,
y: 0,
width: collectionView.bounds.width
- section.sectionInset.left
- section.sectionInset.right,
height: collectionView.bounds.height)
}
item.fillModel(item: cell)
cell.sizeToFit()
returnSize = cell.frame.size
viewCache.setObject(cell, forKey: item.identifa as AnyObject)
}
}
return returnSize ?? CGSize.zero
}
func collectionView(_ collectionView: UICollectionView!, layout collectionViewLayout: UICollectionViewLayout!, heightForHeaderInSection section: Int) -> CGFloat {
var returnFloat: CGFloat?
if section < sections.count,
let item = sections[section].header {
if let cell = (viewCache.object(forKey: item.identifa as AnyObject) ?? item.newItem)
as? UIView {
cell.frame = collectionView.bounds
item.fillModel(item: cell)
cell.sizeToFit()
returnFloat = cell.frame.height
viewCache.setObject(cell, forKey: item.identifa as AnyObject)
}
}
return returnFloat ?? 0
}
func collectionView(_ collectionView: UICollectionView!, layout collectionViewLayout: UICollectionViewLayout!, heightForFooterInSection section: Int) -> CGFloat {
var returnFloat: CGFloat?
if section < sections.count,
let item = sections[section].footer {
if let cell = (viewCache.object(forKey: item.identifa as AnyObject) ?? item.newItem)
as? UIView {
cell.frame = collectionView.bounds
item.fillModel(item: cell)
cell.sizeToFit()
returnFloat = cell.frame.height
viewCache.setObject(cell, forKey: item.identifa as AnyObject)
}
}
return returnFloat ?? 0
}
func collectionView(_ collectionView: UICollectionView!, layout collectionViewLayout: UICollectionViewLayout!, insetForSectionAt section: Int) -> UIEdgeInsets {
if section < sections.count {
return sections[section].sectionInset
}
return UIEdgeInsets.zero
}
func collectionView(_ collectionView: UICollectionView!, layout collectionViewLayout: UICollectionViewLayout!, minimumColumnSpacingForSectionAt section: Int) -> CGFloat {
if section < sections.count {
return CGFloat(sections[section].minimumColumnSpacing)
}
return 0
}
func collectionView(_ collectionView: UICollectionView!, layout collectionViewLayout: UICollectionViewLayout!, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
if section < sections.count {
return CGFloat(sections[section].minimumInteritemSpacing)
}
return 0
}
}