-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTableViewAdapter.swift
More file actions
192 lines (150 loc) · 5.38 KB
/
TableViewAdapter.swift
File metadata and controls
192 lines (150 loc) · 5.38 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
189
190
191
192
//
// UICollectionViewAdapter.swift
// Teby
//
// Created by Mohamed Hashem on 7/16/17.
// Copyright © 2017 MAC. All rights reserved.
//
// swiftlint:disable opening_brace
// swiftlint:disable statement_position
import UIKit
class TableViewAdapter: UITableView, UITableViewDelegate, UITableViewDataSource
{
var dataArray: [Any]!
var cellHeight: CGFloat = 50
private var reuseIdentifier = "Cell"
var autolayoutHeightConstraint: NSLayoutConstraint?
var cellConfigurator: ((UITableViewCell, _ index: IndexPath) -> Void)?
var emptyDataLabel: UIView!
/// cell xib name should match theclass name
func setup<CellType: UITableViewCell>(cellset: String? = nil, data: [Any],
cellHeight: CGFloat, alHeight: NSLayoutConstraint?,
cellConfig: ((CellType, _ index: IndexPath) -> Void)?)
{
dataArray = data
self.cellHeight = cellHeight
autolayoutHeightConstraint = alHeight
cellConfigurator = { (cell: UITableViewCell, _ index: IndexPath) in
if let cellConfig = cellConfig, let cell = cell as? CellType {
cellConfig(cell, index)
}
else
{ print("-- error: couldn't cast cell") }
}
let cell = cellset ?? String(describing: CellType.self)
reuseIdentifier = cell
register(UINib(nibName: cell, bundle: nil), forCellReuseIdentifier: cell)
delegate = self
dataSource = self
}
func setEmptyDataSetView(emptyView: UIView) {
emptyDataLabel = emptyView
emptyDataLabel.isHidden = dataArray.count != 0
}
/// emptyData---
func setEmptyDataLabel(text: String, txtColor: UIColor = #colorLiteral(red: 0.2549019754, green: 0.2745098174, blue: 0.3019607961, alpha: 1), fontSize: CGFloat = 15, font: UIFont)
{
let emptyLabel = UILabel()
emptyLabel.textColor = txtColor
emptyLabel.text = text
emptyLabel.font.withSize(fontSize)
emptyLabel.font = font
setEmptyDataLabel(emptyView: emptyLabel)
}
private func setEmptyDataLabel(emptyView: UIView)
{
emptyDataLabel = emptyView
addSubview(emptyDataLabel)
emptyDataLabel.center = center
emptyDataLabel.sizeToFit()
}
override func reloadData()
{
super.reloadData()
if let emptyDataLabel = emptyDataLabel {
emptyDataLabel.isHidden = dataArray.count != 0
}
}
func reloadData(newData: [Any])
{
dataArray = newData
reloadData()
}
override func layoutSubviews() {
super.layoutSubviews()
if let emptyDataLabel = emptyDataLabel {
emptyDataLabel.center = CGPoint(x: center.x, y: height/2)
}
}
///---
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return dataArray.count
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
return cellHeight
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = dequeueReusableCell(withIdentifier: reuseIdentifier)!
cellConfigurator?(cell, indexPath)
return cell
}
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 = superview?.superview
if animated && topView != nil
{
UIView.animate(withDuration: 0.5, animations:
{ [unowned self] in
autolayoutHeightConstraint.constant = self.contentSize.height
topView?.layoutIfNeeded()
})
} else
{
autolayoutHeightConstraint.constant = contentSize.height
}
}
}
}
class Client: UIViewController
{
@IBOutlet weak var tableViewSample: TableViewAdapter!
@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
tableViewSample.setEmptyDataLabel(label: "No data")
// suting up the collectionViewAdapter
tableViewSample.setup(cell: "sampleCollectionCell", data: dataArray ?? [], cellHeight: 50,
alHeight: constCollectionHeight /* optional */ )
{ (cell, index) in
(cell as! sampleTableViewCell).setup(data: self.tableViewSample.dataArray[index.row] as! String)
}
}
@IBAction func addItemClicked(_ sender: Any)
{
self.tableViewSample.addItem(item: "hello", animated: true)
}
@IBAction func removeItemClicked(_ sender: Any)
{
self.tableViewSample.removeItem(at: IndexPath(row: 1, section: 0), animated: true)
}
}