Repository navigation
Expand file tree
/
Copy pathContactTableViewController.swift
More file actions
183 lines (133 loc) · 5.73 KB
/
Copy pathContactTableViewController.swift
File metadata and controls
183 lines (133 loc) · 5.73 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
//
// ContactTableViewController.swift
// MyContacts
//
// Created by Charles Konkol on 2015-10-12.
// Copyright © 2015 Chuck Konkol. All rights reserved.
//
import UIKit
//0) Add Import Statements for CoreDate and Foundation
import CoreData
import Foundation
class ContactTableViewController: UITableViewController {
//2) Add variable to hold NSManagedObject
var contactArray = [NSManagedObject]()
//3) Add viewDidAppear (loads whenever view appears)
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
loaddb()
}
//4) Add func loaddb to load database and refresh table
func loaddb()
{
let appDelegate =
UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
let fetchRequest = NSFetchRequest(entityName:"Contact")
var error: NSError?
do {
let fetchedResults = try managedContext.executeFetchRequest(fetchRequest) as? [NSManagedObject]
if let results = fetchedResults {
contactArray = results
tableView.reloadData()
} else {
print("Could not fetch \(error), \(error!.userInfo)")
}
} catch let error as NSError {
// failure
print("Fetch failed: \(error.localizedDescription)")
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
//5) Change to return 1
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
//6) Change to return contactArray.count
return contactArray.count
//return 0
}
//7) Uncomment & Change to below to load rows
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
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
}
//8) 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
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
if segue.identifier == "UpdateContacts" {
if let destination = segue.destinationViewController as?
ViewController {
if let SelectIndex = tableView.indexPathForSelectedRow?.row {
let selectedDevice:NSManagedObject = contactArray[SelectIndex] as NSManagedObject
destination.contactdb = selectedDevice
}
}
}
}
}