-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathStudentEditViewController.swift
190 lines (142 loc) · 6.38 KB
/
StudentEditViewController.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
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
import UIKit
class StudentEditViewController: UITableViewController {
let Cell = "Cell"
var dataSource = SectionsArray()
var student: Students!
var nameTextField: UITextField!
var placeTextField: UITextField!
var saveBlock: ((student: Students) -> Void)?
var cancelBlock: (() -> Void)?
init(student: Students?) {
super.init(nibName:nil, bundle:nil)
self.title = "Students"
if let _student = student {
self.student = _student
self.title = "Edit Student"
}
else {
self.student = Students()
self.title = "Add Student"
}
}
required init(coder aDecoder: NSCoder) {
super.init(coder : aDecoder)
}
override func loadView() {
self.tableView = UITableView( frame: UIScreen.mainScreen().bounds, style:.Grouped)
self.tableView.dataSource = self
self.tableView.delegate = self
}
override func viewDidLoad() {
super.viewDidLoad()
if let block = cancelBlock {
self.navigationItem.leftBarButtonItem = UIBarButtonItem(
barButtonSystemItem: .Cancel,
target: self,
action: "dismissAction:"
)
}
self.navigationItem.rightBarButtonItem = UIBarButtonItem(
barButtonSystemItem: .Save,
target: self,
action: "saveAction:"
)
nameTextField = UITextField(frame: CGRectZero)
nameTextField.textAlignment = .Left
nameTextField.clearButtonMode = .WhileEditing
nameTextField.clearButtonMode = .WhileEditing
nameTextField.keyboardType = UIKeyboardType.Default
nameTextField.returnKeyType = .Done
nameTextField.backgroundColor = UIColor.whiteColor()
nameTextField.textColor = UIColor.darkGrayColor()
nameTextField.contentVerticalAlignment = .Center
nameTextField.adjustsFontSizeToFitWidth = true
nameTextField.minimumFontSize = 14.0
nameTextField.autocapitalizationType = .None
nameTextField.autocorrectionType = .No
nameTextField.placeholder = "enter name"
placeTextField = UITextField(frame: CGRectZero)
placeTextField.textAlignment = .Left
placeTextField.clearButtonMode = .WhileEditing
placeTextField.clearButtonMode = .WhileEditing
placeTextField.keyboardType = UIKeyboardType.Default
placeTextField.returnKeyType = .Done
placeTextField.backgroundColor = UIColor.whiteColor()
placeTextField.textColor = UIColor.darkGrayColor()
placeTextField.contentVerticalAlignment = .Center
placeTextField.adjustsFontSizeToFitWidth = true
placeTextField.minimumFontSize = 14.0
placeTextField.autocapitalizationType = .None
placeTextField.autocorrectionType = .No
placeTextField.placeholder = "enter name"
nameTextField.text = student.name
placeTextField.text = student.college
updateDataSource()
}
func updateDataSource() {
var dictionary = Content()
var sections = SectionsArray()
var rows = RowsArray()
dictionary = ["type" : "view", "text" : "Name", "view" : nameTextField]
rows.append(Rows(content: dictionary))
dictionary = ["type" : "view", "text" : "Place", "view" : placeTextField]
rows.append(Rows(content: dictionary))
sections.append(Sections(title: nil, rows: rows))
dataSource = sections
}
// MARK: Selector Methods
func saveAction(sender: AnyObject?) {
self.view.endEditing(true)
let nameStr = self.nameTextField.text ?? String()
let placeStr = self.placeTextField.text ?? String()
if nameStr.isEmpty {
let alertController = UIAlertController(title: "Barbero", message: "missing data", preferredStyle: .Alert)
let alertAction = UIAlertAction(title: "OK", style: .Cancel, handler: nil)
alertController.addAction(alertAction)
self.navigationController!.presentViewController(alertController, animated: true, completion: nil)
return
}
self.student.name = nameStr
self.student.college = placeStr
if let block = saveBlock {
block(student:self.student)
}
}
func dismissAction(sender: AnyObject?) {
if let block = cancelBlock {
block()
}
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return dataSource.count
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataSource[section].rows.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let row = dataSource[indexPath.section].rows[indexPath.row]
let cellType = row.content["type"] as? String ?? String()
if cellType == "view" {
var cell = UITableViewCell(style: .Value1, reuseIdentifier: Cell) as UITableViewCell!
if cell == nil {
cell = UITableViewCell(style: .Default, reuseIdentifier: Cell)
cell.textLabel!.font = UIFont.systemFontOfSize(15.0)
cell.detailTextLabel!.font = UIFont.systemFontOfSize(15.0)
}
let contentSize = tableView.frame.size
let contentWidth = contentSize.width - 30.0
let accessoryWidth = contentWidth - 120.0
let accessoryView = row.content["view"] as UIView!
accessoryView.frame = CGRectMake(0.0, 0.0, accessoryWidth, 34.0)
cell.textLabel!.text = row.content["text"] as String!
cell.accessoryView = accessoryView
cell.selectionStyle = .None
return cell
}
return UITableViewCell(style: .Default, reuseIdentifier: nil)
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
}