-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTableViewController.swift
More file actions
173 lines (129 loc) · 4.94 KB
/
TableViewController.swift
File metadata and controls
173 lines (129 loc) · 4.94 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
//
// TableViewController.swift
// JSONBrowser2
//
// Created by steig hallquist on 3/28/16.
// Copyright © 2016 steig hallquist. All rights reserved.
//
import UIKit
class TableViewController: UITableViewController {
var source: AnyObject?
var values = [(key:String, anObj:AnyObject,shown:Bool,level:Int)]()
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.cellLayoutMarginsFollowReadableWidth = false
//source = ["greet":"hello","values":["one",["a":"two","b":"four"],"three"]]
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
if let newSource = getDictionary(source){
for (key,value) in newSource {
values.append((key,value,false,0))
}
}
}
override func viewDidDisappear(animated: Bool) {
super.viewDidDisappear(animated)
values.removeAll()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return values.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as? TableViewCell{
var text = ""
let aValue = values[indexPath.row]
let title = aValue.shown ? "-" : "+"
cell.button.setTitle(title, forState: .Normal)
cell.buttonLeading.constant = CGFloat(values[indexPath.row].level) * 5.0
cell.fxShowHideRow = nil
cell.button.hidden = true
switch aValue.anObj {
case is NSArray, is NSDictionary:
cell.fxShowHideRow = self.showHideRow
cell.button.hidden = false
text = "{\(getNumRows(aValue.anObj))}"
case is NSString:
text = aValue.anObj as! String
case is NSNumber:
text = (aValue.anObj as! NSNumber).stringValue
case is NSNull:
text = "Null"
default:
text = ""
}
cell.label.text = "\(aValue.key) : \(text)"
return cell
}
return UITableViewCell()
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 28
}
override func tableView(tableView: UITableView, shouldHighlightRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return false
}
func showHideRow(show:Bool, cell:UITableViewCell){
let indexPath = self.tableView.indexPathForCell(cell)
if indexPath != nil {
if !values[indexPath!.row].shown {
expandRow(indexPath!.row)
} else {
collapseRow(indexPath!.row)
}
}
}
func expandRow(row:Int) {
values[row].shown = true
let level = values[row].level + 1
let newValues = getDictionary(values[row].anObj) ?? [values[row].key:values[row].anObj]
var indx = 0
for (key,value) in newValues{
values.insert((key,value,false,level), atIndex: row + 1 + indx)
indx += 1
}
self.tableView.reloadData()
}
func collapseRow(row:Int){
values[row].shown = false
for _ in 0..<getNumRows(values[row].anObj) {
let cVal = values[row + 1]
if cVal.shown {collapseRow(row + 1)}
values.removeAtIndex(row + 1)
}
self.tableView.reloadData()
}
func getNumRows(anObj:AnyObject)->Int{
switch anObj {
case is NSDictionary:
return (anObj as! NSDictionary).count
case is NSArray:
return (anObj as! NSArray).count
default:
return 0
}
}
func getDictionary(source:AnyObject?) -> [String:AnyObject]?{
var newSource: [String:AnyObject]?
if source is [String:AnyObject] {
newSource = source as? [String:AnyObject]
} else if source is [AnyObject] {
newSource = [String:AnyObject]()
for (indx, anObj) in (source as? NSArray)!.enumerate(){
newSource!["\(indx)"] = anObj
}
} else {
newSource = nil
}
return newSource
}
}