-
Notifications
You must be signed in to change notification settings - Fork 2
/
ReusingContainerView.swift
163 lines (128 loc) · 4.54 KB
/
ReusingContainerView.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
//
// ReusingContainerView.swift
// UniLayout Example
//
// Contains the reusing container and data to supply to the reusable views inside (based on UITableView)
//
import UIKit
import UniLayout
enum ReusableItemType: String {
case unknown = ""
case section = "section"
case item = "item"
case topDivider = "topDivider"
case bottomDivider = "bottomDivider"
}
class ReusableItem {
let type: ReusableItemType
let title: String
let additional: String
let value: String
init(type: ReusableItemType, title: String = "", additional: String = "", value: String = "") {
self.type = type
self.title = title
self.additional = additional
self.value = value
}
}
class ReusingContainerView: UniFrameContainer, UITableViewDataSource, UITableViewDelegate {
// --
// MARK: Members
// --
var items: [ReusableItem] = []
let container = UniReusingContainer()
// --
// MARK: Initialize
// --
override init(frame : CGRect) {
super.init(frame: frame)
initialize()
}
convenience init() {
self.init(frame: CGRect.zero)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
initialize()
}
func initialize () {
// Set up the reusing container
container.layoutProperties.width = UniLayoutProperties.stretchToParent
container.layoutProperties.height = UniLayoutProperties.stretchToParent
container.backgroundColor = UIColor(white: 0.95, alpha: 1)
addSubview(container)
// Add empty footer
let tableFooter = UIView()
tableFooter.frame = CGRect(x: 0, y: 0, width: 0, height: 0.01)
container.tableFooterView = tableFooter
// Set the container properties
container.rowHeight = UITableViewAutomaticDimension
container.estimatedRowHeight = 40
container.dataSource = self
container.delegate = self
container.separatorStyle = .none
}
// --
// MARK: Modify items
// --
func removeItems() {
items.removeAll()
}
func addItem(_ item: ReusableItem) {
items.append(item)
}
// --
// MARK: UITableViewDataSource
// --
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Create cell (if needed)
let item = items[(indexPath as NSIndexPath).row]
let nextType = (indexPath as NSIndexPath).row + 1 < items.count ? items[(indexPath as NSIndexPath).row + 1].type : ReusableItemType.unknown
let cell = tableView.dequeueReusableCell(withIdentifier: item.type.rawValue) as? UniReusableView ?? UniReusableView()
cell.isSimulatingSpacing = false
// Set up an item cell
if item.type == .item {
// Create view
if !(cell.view is ItemView) {
cell.view = ItemView()
}
let cellView = cell.view as? ItemView
// Supply data
cell.selectionStyle = .none
cell.dividerLine.isHidden = nextType != .item
cell.contentView.backgroundColor = UIColor.white
cellView?.title = item.title
cellView?.additional = item.additional
cellView?.value = item.value
}
// Set up a section cell
if item.type == .section {
// Create view
if !(cell.view is SectionView) {
cell.view = SectionView()
}
let cellView = cell.view as? SectionView
// Supply data
cell.selectionStyle = .none
cell.dividerLine.isHidden = true
cell.contentView.backgroundColor = tableView.backgroundColor
cellView?.text = item.title
}
// Set up a divider cell
if item.type == .topDivider || item.type == .bottomDivider {
// Create view
if !(cell.view is DividerView) {
cell.view = DividerView(bottom: item.type != .topDivider)
}
// Supply data
cell.selectionStyle = .none
cell.dividerLine.isHidden = true
cell.contentView.backgroundColor = tableView.backgroundColor
}
// Return result
return cell
}
}