-
Notifications
You must be signed in to change notification settings - Fork 0
/
LXKAreaPicker.swift
425 lines (346 loc) · 14.8 KB
/
LXKAreaPicker.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
//
// LXKAreaPicker.swift
// SwiftPlayground
//
// Created by 李现科 on 16/5/17.
// Copyright © 2016年 李现科. All rights reserved.
//
import UIKit
protocol LXKAreaPickerDataSource {
associatedtype T: LXKAreaPickerElement
func numberOfSectionsInAreaPicker(areaPicker: LXKAreaPicker<T>) -> Int
func areaPicker(areaPicker: LXKAreaPicker<T>, itemsInSection section: Int) -> [T]
}
protocol LXKAreaPickerElement {
var title: String? { get }
}
/// 选择区域,自动覆盖下级目录
struct SeletedAreaItems<T: LXKAreaPickerElement> {
private (set) var rawValue: Array<T>
init(_ rawValue: [T]) {
self.rawValue = rawValue
}
init() {
rawValue = [T]()
}
subscript(index: Int) -> T {
get {
return rawValue[index]
}
set {
if rawValue.endIndex > index + 1 {
let range = index + 1...rawValue.endIndex.predecessor()
rawValue.removeRange(range)
} else if index == rawValue.endIndex {
rawValue.append(newValue)
return
} else if index > rawValue.endIndex {
fatalError("should add previous items firstly")
}
rawValue[index] = newValue
}
}
}
private let tableViewCellIdentifier = "tableViewCellIdentifier"
/// 标题栏文字大小
private let titleFontSize: CGFloat = 14.0
/// 标题栏文字颜色
private let titleColor = UIColor.lightGrayColor()
/// 条目文字颜色
private let itemFontSize: CGFloat = 13.0
/// 条目颜色
private let itemColor = UIColor.lightGrayColor()
@IBDesignable
class LXKAreaPicker<T: LXKAreaPickerElement>: UIView, UITableViewDataSource, UITableViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
private var titleCollectionView: UICollectionView!
private var itemCollectionView: UICollectionView!
private var seperatorView: UIView!
private let titleCollectionViewCellIdentifier = "titleCollectionViewCellIdentifier"
private let itemCollectionViewCellIdentifier = "itemCollectionViewCellIdentifier"
var numberOfSections: ((areaPicker: LXKAreaPicker) -> Int)?
var itemsInSection: ((section: Int) -> [T])?
var didSelectItem: ((indexPath: NSIndexPath) -> ())?
private var selectedItems = SeletedAreaItems<T>()
/// 顶部栏高度
@IBInspectable
private let topBarHeight: CGFloat = 44.0
// MARK: - init
override init(frame: CGRect) {
super.init(frame: frame)
setUpSubviews()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setUpSubviews()
}
override func layoutSubviews() {
super.layoutSubviews()
_ = {
var collectionViewFrame = bounds
collectionViewFrame.size.height = topBarHeight
let flowLayout = titleCollectionView.collectionViewLayout as! UICollectionViewFlowLayout
flowLayout.itemSize = collectionViewFrame.size
titleCollectionView.frame = collectionViewFrame
}()
_ = {
let seperatorViewFrame = CGRect(x: 0.0, y: topBarHeight, width: bounds.size.width, height: 1.0)
seperatorView.frame = seperatorViewFrame
}()
_ = {
var collectionViewFrame = bounds
collectionViewFrame.origin.y = topBarHeight + 1.0
collectionViewFrame.size.height = bounds.size.height - topBarHeight - 1.0
let flowLayout = itemCollectionView.collectionViewLayout as! UICollectionViewFlowLayout
flowLayout.itemSize = collectionViewFrame.size
itemCollectionView.frame = collectionViewFrame
}()
}
/// 初始化控件
func setUpSubviews() {
titleCollectionView = {
var collectionViewFrame = bounds
collectionViewFrame.size.height = topBarHeight
let flowLayout = UICollectionViewFlowLayout()
flowLayout.itemSize = collectionViewFrame.size
flowLayout.minimumInteritemSpacing = 16.0
flowLayout.scrollDirection = .Horizontal
let collectionView = UICollectionView(frame: collectionViewFrame, collectionViewLayout: flowLayout)
collectionView.registerClass(TitleCollectionViewCell.self, forCellWithReuseIdentifier: titleCollectionViewCellIdentifier)
collectionView.contentInset = UIEdgeInsets(top: 0.0, left: 16.0, bottom: 0.0, right: 0.0)
collectionView.scrollsToTop = false
collectionView.showsHorizontalScrollIndicator = false
collectionView.showsVerticalScrollIndicator = false
collectionView.backgroundColor = .whiteColor()
collectionView.dataSource = self
collectionView.delegate = self
return collectionView
}()
seperatorView = {
let view = UIView(frame: CGRect(x: 0.0, y: topBarHeight, width: bounds.size.width, height: 1.0))
view.backgroundColor = UIColor(red: 0xAA/255.0, green: 0xAA/255.0, blue: 0xAA/255.0, alpha: 1.0)
return view
}()
itemCollectionView = {
var collectionViewFrame = bounds
collectionViewFrame.origin.y = topBarHeight + 1.0
collectionViewFrame.size.height = bounds.size.height - topBarHeight - 1.0
let flowLayout = UICollectionViewFlowLayout()
flowLayout.itemSize = collectionViewFrame.size
flowLayout.minimumInteritemSpacing = 0.0
flowLayout.minimumLineSpacing = 0.0
flowLayout.scrollDirection = .Horizontal
let collectionView = UICollectionView(frame: collectionViewFrame, collectionViewLayout: flowLayout)
collectionView.registerClass(ItemCollectionViewCell<T>.self, forCellWithReuseIdentifier: itemCollectionViewCellIdentifier)
collectionView.scrollsToTop = false
collectionView.pagingEnabled = true
collectionView.showsHorizontalScrollIndicator = false
collectionView.showsVerticalScrollIndicator = false
collectionView.backgroundColor = .whiteColor()
collectionView.dataSource = self
collectionView.delegate = self
return collectionView
}()
addSubview(titleCollectionView)
addSubview(seperatorView)
addSubview(itemCollectionView)
}
func dismiss() {
removeFromSuperview()
}
/// 刷新数据
func reloadData() {
titleCollectionView.reloadData()
itemCollectionView.reloadData()
}
/// 获取选中的item
func seletedItemInSection(section: Int) -> T? {
guard section >= 0 && section < selectedItems.rawValue.count else {
return nil
}
return selectedItems[section]
}
// MARK: - UITableViewDataSource,UITableViewDelegate
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let cell = tableView.superview as? UICollectionViewCell {
if let indexPath = itemCollectionView.indexPathForCell(cell) {
return itemsInSection?(section: indexPath.row).count ?? 0
}
}
return 0
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 44.0
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(tableViewCellIdentifier, forIndexPath: indexPath) as! ItemTableViewCell
if let collectionViewCell = tableView.superview as? UICollectionViewCell {
if let collectionIndexPath = itemCollectionView.indexPathForCell(collectionViewCell) {
let item = itemsInSection?(section: collectionIndexPath.row)[indexPath.row] as? LXKAreaPickerElement
cell.item = item
if selectedItems.rawValue.count > collectionIndexPath.row {
if selectedItems.rawValue[collectionIndexPath.row].title == item?.title {
cell.accessoryType = .Checkmark
}
}
}
}
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if let cell = tableView.superview as? UICollectionViewCell {
if let collectionIndexPath = itemCollectionView.indexPathForCell(cell) {
if let item = itemsInSection?(section: collectionIndexPath.row)[indexPath.row] {
let seletedIndexPath = NSIndexPath(forRow: indexPath.row, inSection: collectionIndexPath.row)
selectedItems[collectionIndexPath.row] = item
reloadData()
let nextIndex = min(collectionIndexPath.row + 1, (numberOfSections?(areaPicker: self) ?? 1) - 1)
itemCollectionView.scrollToItemAtIndexPath(NSIndexPath(forRow: nextIndex, inSection: 0), atScrollPosition: .None, animated: true)
didSelectItem?(indexPath: seletedIndexPath)
}
}
}
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
// MARK: UICollectionViewDataSource, UICollectionViewDelegate
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return min(selectedItems.rawValue.count + 1,numberOfSections?(areaPicker: self) ?? 0)
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
switch collectionView {
case titleCollectionView:
let title = indexPath.row < selectedItems.rawValue.count ? (selectedItems[indexPath.row].title ?? "未知") : "请选择"
let rect = (title as NSString).boundingRectWithSize(CGSize(width: CGFloat.max, height: CGFloat.max), options: [.UsesLineFragmentOrigin,.UsesFontLeading], attributes: [NSFontAttributeName:UIFont.systemFontOfSize(titleFontSize)], context: nil)
return CGSize(width: CGRectGetWidth(rect) + 4.0, height: topBarHeight)
case itemCollectionView:
return collectionView.bounds.size
default:
return CGSize.zero
}
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
switch collectionView {
case titleCollectionView:
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(titleCollectionViewCellIdentifier, forIndexPath: indexPath) as! TitleCollectionViewCell
let title = indexPath.row < selectedItems.rawValue.count ? (selectedItems[indexPath.row].title ?? "未知") : "请选择"
cell.titleLabel.text = title
return cell
case itemCollectionView:
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(itemCollectionViewCellIdentifier, forIndexPath: indexPath) as! ItemCollectionViewCell<T>
cell.tableView.dataSource = self
cell.tableView.delegate = self
return cell
default:
let cell = UICollectionViewCell()
return cell
}
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
if collectionView == titleCollectionView {
itemCollectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: .None, animated: true)
}
}
}
class TitleCollectionViewCell: UICollectionViewCell {
var titleLabel: UILabel!
override init(frame: CGRect) {
super.init(frame: frame)
setUpSubviews()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setUpSubviews()
}
override func layoutSubviews() {
super.layoutSubviews()
titleLabel.frame = bounds
}
func setUpSubviews() {
titleLabel = {
let label = UILabel(frame: bounds)
label.font = UIFont.systemFontOfSize(titleFontSize)
label.textColor = titleColor
return label
}()
addSubview(titleLabel)
}
}
class ItemCollectionViewCell<T: LXKAreaPickerElement>: UICollectionViewCell {
var tableView: UITableView!
override init(frame: CGRect) {
super.init(frame: frame)
setUpSubviews()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setUpSubviews()
}
override func layoutSubviews() {
super.layoutSubviews()
tableView.frame = bounds
}
func setUpSubviews() {
tableView = {
let tableView = UITableView(frame: bounds)
tableView.registerClass(ItemTableViewCell.self, forCellReuseIdentifier: tableViewCellIdentifier)
tableView.tableFooterView = UIView()
return tableView
}()
addSubview(tableView)
}
override func prepareForReuse() {
tableView.delegate = nil
tableView.dataSource = nil
tableView.reloadData()
}
}
class ItemTableViewCell: UITableViewCell {
var itemLabel: UILabel!
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setUpSubviews()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setUpSubviews()
}
override func layoutSubviews() {
super.layoutSubviews()
var labelFrame = bounds
labelFrame.origin.x = 16.0
itemLabel.frame = labelFrame
}
func setUpSubviews() {
itemLabel = {
var labelFrame = bounds
labelFrame.origin.x = 16.0
let label = UILabel(frame: labelFrame)
label.font = UIFont.systemFontOfSize(itemFontSize)
label.textColor = itemColor
return label
}()
addSubview(itemLabel)
}
var item: LXKAreaPickerElement? {
didSet {
itemLabel.text = item?.title ?? "未知"
}
}
override func prepareForReuse() {
super.prepareForReuse()
accessoryType = .None
}
}
extension UIColor {
convenience init(rgb: Int,alpha: CGFloat = 1.0) {
let r = CGFloat(rgb / 0x10000) / 255.0
let g = CGFloat(rgb % 0x10000 / 0x100) / 255.0
let b = CGFloat(rgb % 0x100) / 255.0
self.init(red: r, green: g, blue: b, alpha: alpha)
}
}