-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollectionViewAdapter.swift
More file actions
196 lines (139 loc) · 5.54 KB
/
CollectionViewAdapter.swift
File metadata and controls
196 lines (139 loc) · 5.54 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
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
//
// CollectionViewAdapter.swift
//
// Created by Mohamed Hashem on 7/16/17.
// Copyright © 2017 MAC. All rights reserved.
//
import UIKit
class CollectionViewAdapter: UICollectionView , UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
var dataArray : Array<Any>!
var cellSize : CGSize!
private var reuseIdentifier = "Cell"
var AutolayoutHeightConstraint:NSLayoutConstraint?
var didSelectItemAt: ((_ index:IndexPath)->())?
var CellConfigurator : ((UICollectionViewCell,_ index:IndexPath)->())?
var emptyDataLabel:UILabel=UILabel()
/// cell xib name should match theclass name
func setup(cell:String,data:Array<Any>,cellsize:CGSize,AL_Height:NSLayoutConstraint?,cellConfig:((UICollectionViewCell,_ index:IndexPath)->())?)
{
dataArray = data
cellSize = cellsize
AutolayoutHeightConstraint = AL_Height
CellConfigurator = cellConfig
reuseIdentifier = cell;
self.register(UINib(nibName: cell, bundle: nil), forCellWithReuseIdentifier: cell)
self.delegate = self
self.dataSource = self
}
/// emptyData---
func SetEmptyDataLabel(label:String)
{
emptyDataLabel.textColor = #colorLiteral(red: 0.2549019754, green: 0.2745098174, blue: 0.3019607961, alpha: 1)
emptyDataLabel.text = label
addSubview(emptyDataLabel)
emptyDataLabel.center = center
emptyDataLabel.sizeToFit()
}
override func reloadData()
{
super.reloadData()
emptyDataLabel.isHidden = dataArray.count != 0
}
override func layoutSubviews() {
super.layoutSubviews()
emptyDataLabel.center = CGPoint(x: center.x, y: height/2)
}
///---
// MARK: UICollectionViewDataSource
func numberOfSections(in collectionView: UICollectionView) -> Int
{
// #warning Incomplete implementation, return the number of sections
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of items
return dataArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)
CellConfigurator?(cell,indexPath)
// Configure the cell
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
{
return cellSize
}
func AddItem(item:Any,Animated:Bool)
{
dataArray.append(item)
self.reloadData()
Stretch(Animated: Animated)
}
func removeItem(at index: IndexPath,Animated:Bool)
{
dataArray.remove(at: index.row)
self.reloadData()
Stretch(Animated: Animated)
}
func Stretch(Animated:Bool) {
if let AutolayoutHeightConstraint = AutolayoutHeightConstraint
{
let topView = self.topMostController()?.view
if Animated && topView != nil
{
UIView.animate(withDuration:0.5, animations:
{
AutolayoutHeightConstraint.constant = self.collectionViewLayout.collectionViewContentSize.height
topView?.layoutIfNeeded()
})
}
else
{
AutolayoutHeightConstraint.constant = self.collectionViewLayout.collectionViewContentSize.height
}
}
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
didSelectItemAt?(indexPath)
}
}
// usage
class sampleCollectionCell: UICollectionViewCell {
@IBOutlet weak var txtLabel:UILabel!
override func awakeFromNib()
{
super.awakeFromNib()
}
func setup(data:String)
{
txtLabel.text = data
}
}
class client: UIViewController
{
@IBOutlet weak var collectionViewSample: CollectionViewAdapter!
@IBOutlet weak var ConstCollectionHeight : NSLayoutConstraint!// constraint of the collection view Height / if you ont want animation ignore this line
var dataArray : [String]? = ["hi","wow"] // if there is no initial data, you can ignore this line
override func viewDidLoad()
{
super.viewDidLoad()
// empty data label that show when there is no data
collectionViewSample.SetEmptyDataLabel(label: "No data")
// suting up the collectionViewAdapter
let cellsize = CGSize(width:(collectionViewSample.width-10) / 2,height: 50)
collectionViewSample.setup(cell: "sampleCollectionCell", data: dataArray ?? [], cellsize: cellsize, AL_Height: ConstCollectionHeight /* optional */ )
{ (cell, index) in
(cell as! sampleCollectionCell).setup(data: self.collectionViewSample.dataArray[index.row] as! String)
}
}
@IBAction func addItemClicked(_ sender: Any)
{
self.collectionViewSample.AddItem(item: "hello", Animated: true)
}
@IBAction func removeItemClicked(_ sender: Any)
{
self.collectionViewSample.removeItem(at: IndexPath(row: 1, section: 0), Animated: true)
}
}