Minimal working version
Seto Elkahfi committed
Mar 27, 2017 at 21:13 UTC
4b085d95834ff34d706f7a92d8b7ff2e3f55ea08
3 files changed
+80
-4
JomloTableView/Classes/JomloTableView.swift
+33
-3
@@ -13,12 +13,42 @@ public class JomloTableView: UITableView {
13
// Table view section
14
var sections = Array<JomloTableViewSection>()
15
16
-
16
+
17
+ func numberOfSectionsInTableView(tableView: UITableView) -> Int {
18
+ return sections.count
19
+ }
20
+
21
+ // Add single section to table view
22
+ func addSection(section: JomloTableViewSection) {
23
+ sections.append(section)
24
+ }
25
+
26
+ // Remove all section in table view
27
+ func removeAllSection() {
28
+ sections.removeAll()
29
+ }
30
31
}
32
33
extension JomloTableView: UITableViewDelegate {
34
35
+ public func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
36
+ return sections[indexPath.section].rows[indexPath.row].estimatedRowHeight
37
+ }
38
+
39
+ public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
40
+ return sections[indexPath.section].rows[indexPath.row].rowHeight
41
+ }
42
+
43
+ public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
44
+ let item = sections[indexPath.section].rows[indexPath.row]
45
+ item.onItemClicked()
46
+ }
47
+
48
+ public func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
49
+ sections[indexPath.section].rows[indexPath.row].willDisplay(cell: cell)
50
+ }
51
+
52
}
53
54
extension JomloTableView: UITableViewDataSource {
@@ -35,10 +65,10 @@ extension JomloTableView: UITableViewDataSource {
65
let item = sections[indexPath.section].rows[indexPath.row]
66
let identifier = item.identifier
67
38
- var cell = tableView.dequeueReusableCell(withIdentifier: identifier) as? SdTableViewCell
68
+ var cell = tableView.dequeueReusableCell(withIdentifier: identifier) as? JomloTableViewCell
69
if cell == nil {
70
tableView.register(UINib(nibName: identifier, bundle: nil), forCellReuseIdentifier: identifier)
41
- cell = tableView.dequeueReusableCell(withIdentifier: identifier) as? SdTableViewCell
71
+ cell = tableView.dequeueReusableCell(withIdentifier: identifier) as? JomloTableViewCell
72
}
73
74
item.populateView(cell: cell!)
JomloTableView/Classes/JomloTableViewRow.swift
+36
-1
@@ -2,7 +2,7 @@
2
// JomloTableViewRow.swift
3
// Pods
4
//
5
-// Created by SDMobile on 3/27/17.
5
+// Created by Seto Elkahfi on 3/27/17.
6
//
7
//
8
@@ -10,4 +10,39 @@ import Foundation
10
11
public class JomloTableViewRow: NSObject {
12
13
+ var identifier: String {
14
+ return ""
15
+ }
16
+
17
+ var rowHeight: CGFloat {
18
+ return 0
19
+ }
20
+
21
+ var estimatedRowHeight: CGFloat {
22
+ return 0
23
+ }
24
+
25
+ // Callback for on row clicked.
26
+ // Use IBAction on single view componen if you need to register action for a particular view inside the row
27
+ internal var onClicked: ((JomloTableViewRow) -> (Void))?
28
+
29
+ func setOnItemClickedCallback(clicked:@escaping (JomloTableViewRow) -> Void){
30
+ onClicked = clicked
31
+ }
32
+
33
+
34
+ func populateView(cell: JomloTableViewCell) {
35
+ fatalError("Should be overrided")
36
+ }
37
+
38
+ func willDisplay(cell: UITableViewCell){
39
+ cell.separatorInset = UIEdgeInsetsMake(0, 6, 0, 6)
40
+ }
41
+
42
+ func onItemClicked() {
43
+ if let clicked = onClicked {
44
+ clicked(self)
45
+ }
46
+ }
47
+
48
}
JomloTableView/Classes/JomloTableViewSection.swift
+11
@@ -12,10 +12,21 @@ public class JomloTableViewSection {
12
13
var rows = Array<JomloTableViewRow>()
14
15
+ // Count rows in this table view section
16
var count: Int {
17
get {
18
return rows.count
19
}
20
}
21
22
+ // Add a single row for this table view section
23
+ func addRow(row: JomloTableViewRow) {
24
+ self.rows.append(row);
25
+ }
26
+
27
+ // Remove all rows in this table view section
28
+ func removeAllRows() {
29
+ self.rows.removeAll()
30
+ }
31
+
32
}