Repository navigation
Expand file tree
/
Copy pathContactTableViewController.swift
More file actions
263 lines (207 loc) · 8.49 KB
/
Copy pathContactTableViewController.swift
File metadata and controls
263 lines (207 loc) · 8.49 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
//
// ContactTableViewController.swift
// MyContacts
//
// Created by Charles Konkol on 2016-05-05
// Copyright © 2016 Chuck Konkol. All rights reserved.
//
import UIKit
//0) Add Import Statements for CoreDate and Foundation
//**Begin Copy**
import CoreData
import Foundation
//**End Copy**
//1) Add Delegates to right of UITableViewController
// ,UISearchResultsUpdating, UISearchBarDelegate, UISearchControllerDelegate
class ContactTableViewController: UITableViewController,UISearchResultsUpdating, UISearchBarDelegate, UISearchControllerDelegate {
//2) Add filter search vars
//**Begin Copy**
var filteredTableData = [NSManagedObject]()
var resultSearchController = UISearchController()
//**End Copy**
//3) Add UISearch func
//**Begin Copy**
func updateSearchResultsForSearchController(searchController: UISearchController)
{
filteredTableData.removeAll(keepCapacity: false)
//search for field in CoreData
let searchPredicate = NSPredicate(format: "fullname CONTAINS[c] %@", searchController.searchBar.text!)
let array = (contactArray as NSArray).filteredArrayUsingPredicate(searchPredicate)
filteredTableData = array as! [NSManagedObject]
self.tableView.reloadData()
}
//**End Copy**
//4) Add variable to hold NSManagedObject
//**Begin Copy**
var contactArray = [NSManagedObject]()
//**End Copy**
//3) Add viewDidAppear (loads whenever view appears)
//**Begin Copy**
override func viewWillAppear(animated: Bool) {
super.viewDidAppear(animated)
loaddb()
}
//**End Copy**
//5) Add func loaddb to load database and refresh table
//**Begin Copy**
func loaddb()
{
let appDelegate =
UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
let fetchRequest = NSFetchRequest(entityName:"Contact")
do {
let fetchedResults = try managedContext.executeFetchRequest(fetchRequest) as? [NSManagedObject]
if let results = fetchedResults {
contactArray = results
tableView.reloadData()
} else {
print("Could not fetch")
}
} catch let error as NSError {
// failure
print("Fetch failed: \(error.localizedDescription),\(error.userInfo)")
}
}
//**End Copy**
override func viewDidLoad() {
super.viewDidLoad()
//6 Add Search Delegates
//**Begin Copy**
self.resultSearchController.delegate = self
self.resultSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.delegate = self
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
controller.searchBar.delegate = self
self.tableView.tableHeaderView = controller.searchBar
return controller
})()
}
//**End Copy**
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
//7) Change to return 1
//**Begin Copy**
return 1
//**End Copy**
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
//8) Change to return contactArray.count
//**Begin Copy**
if (self.resultSearchController.active) {
return filteredTableData.count
}
else {
return contactArray.count
}
//**End Copy**
}
//9) Uncomment
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//9a) Change below to load rows
//**Begin Copy**
if (self.resultSearchController.active) {
let cell =
tableView.dequeueReusableCellWithIdentifier("Cell")
as UITableViewCell!
let person = filteredTableData[indexPath.row]
cell.textLabel?.text = person.valueForKey("fullname") as! String?
cell.detailTextLabel?.text = ">>"
return cell
}
else {
let cell =
tableView.dequeueReusableCellWithIdentifier("Cell")
as UITableViewCell!
let person = contactArray[indexPath.row]
cell.textLabel?.text = person.valueForKey("fullname") as! String?
cell.detailTextLabel?.text = ">>"
return cell
}
//**End Copy**
}
//10) Add func tableView to show row clicked
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
print("You selected cell #\(indexPath.row)")
}
//9) Uncomment
// Override to support conditional editing of the table view.
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// Return false if you do not want the specified item to be editable.
return true
}
//10) Uncomment func tableView
// Override to support editing the table view.
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
//11 Change to delete swiped row
if editingStyle == .Delete {
let appDelegate =
UIApplication.sharedApplication().delegate as! AppDelegate
let context = appDelegate.managedObjectContext
if (self.resultSearchController.active) {
context.deleteObject(filteredTableData[indexPath.row])
}
else {
context.deleteObject(contactArray[indexPath.row])
}
var error: NSError? = nil
do {
try context.save()
loaddb()
} catch let error1 as NSError {
error = error1
print("Unresolved error \(error)")
abort()
}
}
}
/*
// Override to support rearranging the table view.
override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {
}
*/
/*
// Override to support conditional rearranging of the table view.
override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// Return false if you do not want the item to be re-orderable.
return true
}
*/
// MARK: - Navigation
// 12) Uncomment prepareforseque
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
//13) Uncomment & Change to go to proper record on proper Viewcontroller
//**Begin Copy**
if segue.identifier == "UpdateContacts" {
if let destination = segue.destinationViewController as?
ViewController {
if (self.resultSearchController.active) {
if let SelectIndex = tableView.indexPathForSelectedRow?.row {
let selectedDevice:NSManagedObject = filteredTableData[SelectIndex] as NSManagedObject
destination.contactdb = selectedDevice
resultSearchController.active = false
}
}
else {
if let SelectIndex = tableView.indexPathForSelectedRow?.row {
let selectedDevice:NSManagedObject = contactArray[SelectIndex] as NSManagedObject
destination.contactdb = selectedDevice
}
}
}
}
}
//**End Copy**
}