-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExampleViewController.swift
executable file
·163 lines (116 loc) · 4.56 KB
/
ExampleViewController.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
//
// ViewController.swift
// SpreadsheetView
//
// Created by Kishikawa Katsumi on 5/18/17.
// Copyright © 2017 Kishikawa Katsumi. All rights reserved.
//
// Modified by K. Kieffer to show example use of
// SingleRowSortableSpreadSheetView.swift
//
import UIKit
import SpreadsheetView
class ViewController: UIViewController, SpreadsheetViewDataSource, SpreadsheetActionsDelegate {
@IBOutlet weak var spreadsheetView: SpreadsheetView!
var header = [String]()
var data = [[String]]()
enum Sorting {
case ascending
case descending
var symbol: String {
switch self {
case .ascending:
return "\u{25B2}"
case .descending:
return "\u{25BC}"
}
}
}
var sortedColumn = (column: 0, sorting: Sorting.ascending)
override func viewDidLoad() {
super.viewDidLoad()
spreadsheetView.dataSource = self
spreadsheetView.delegate = self
spreadsheetView.addTapAndLongPressGestures(withMinLongPressDuration: 0.3)
spreadsheetView.register(HeaderCell.self, forCellWithReuseIdentifier: String(describing: HeaderCell.self))
spreadsheetView.register(TextCell.self, forCellWithReuseIdentifier: String(describing: TextCell.self))
let data = try! String(contentsOf: Bundle.main.url(forResource: "data", withExtension: "tsv")!, encoding: .utf8)
.components(separatedBy: "\r\n")
.map { $0.components(separatedBy: "\t") }
header = data[0]
self.data = Array(data.dropFirst())
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
spreadsheetView.flashScrollIndicators()
}
//A row was selected by the user
func didSelectRow(at row : Int) {
print("Did select row for \(row) \(data[row-1][0])")
}
//A long press was finished on the row by the user
func longPressDidEnd(at row: Int) {
print("Long press at row \(row) for \(data[row-1][0])")
}
//Use the String at column 0 as the unique
func uniqueObject(forRow row: Int) -> Any {
return data[row-1][0]
}
func uniqueObjectsAreEqual(_ obj1: Any, _ obj2: Any) -> Bool {
return obj1 as! String == obj2 as! String
}
// MARK: DataSource
func numberOfColumns(in spreadsheetView: SpreadsheetView) -> Int {
return header.count
}
func numberOfRows(in spreadsheetView: SpreadsheetView) -> Int {
return 1 + data.count
}
func spreadsheetView(_ spreadsheetView: SpreadsheetView, widthForColumn column: Int) -> CGFloat {
return 140
}
func spreadsheetView(_ spreadsheetView: SpreadsheetView, heightForRow row: Int) -> CGFloat {
if case 0 = row {
return 60
} else {
return 24
}
}
func frozenColumns(in spreadsheetView: SpreadsheetView) -> Int {
return 1
}
//The header row is frozen
func frozenRows(in spreadsheetView: SpreadsheetView) -> Int {
return 1
}
func sortBy(column : Int) {
if sortedColumn.column == column {
sortedColumn.sorting = sortedColumn.sorting == .ascending ? .descending : .ascending
} else {
sortedColumn = (column, .ascending)
}
data.sort {
let ascending = $0[sortedColumn.column] < $1[sortedColumn.column]
return sortedColumn.sorting == .ascending ? ascending : !ascending
}
}
func spreadsheetView(_ spreadsheetView: SpreadsheetView, cellForItemAt indexPath: IndexPath) -> Cell? {
if case 0 = indexPath.row {
let cell = spreadsheetView.dequeueReusableCell(withReuseIdentifier: String(describing: HeaderCell.self), for: indexPath) as! HeaderCell
cell.label.text = header[indexPath.column]
if case indexPath.column = sortedColumn.column {
cell.sortArrow.text = sortedColumn.sorting.symbol
} else {
cell.sortArrow.text = ""
}
cell.setNeedsLayout()
return cell
} else {
let cell = spreadsheetView.dequeueReusableCell(withReuseIdentifier: String(describing: TextCell.self), for: indexPath) as! TextCell
cell.label.text = data[indexPath.row - 1][indexPath.column]
cell.isHighlighted = cell.isSelected
cell.setNeedsLayout()
return cell
}
}
}