Repository navigation
Expand file tree
/
Copy pathNotesTableViewController.swift
More file actions
184 lines (115 loc) · 5.66 KB
/
Copy pathNotesTableViewController.swift
File metadata and controls
184 lines (115 loc) · 5.66 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
//
// NotesTableViewController.swift
// WatchNotes
//
// Created by Mohammad AZam on 11/22/14.
// Copyright (c) 2014 AzamSharp Consulting LLC. All rights reserved.
//
import UIKit
class NotesTableViewController: UITableViewController,UITextFieldDelegate,NSFilePresenter {
var notes :NSMutableArray = NSMutableArray()
var fileCoordinator = NSFileCoordinator()
override func viewDidLoad() {
super.viewDidLoad()
// delete all existing notes
deleteAllNotes()
// populate the notes and update the tableView
populateNotes()
}
var presentedItemURL: NSURL? {
let groupURL = NSFileManager.defaultManager().containerURLForSecurityApplicationGroupIdentifier("group.watchnotes")
let fileURL = groupURL?.URLByAppendingPathComponent("notes.bin")
return fileURL!
}
var presentedItemOperationQueue: NSOperationQueue {
return NSOperationQueue.mainQueue()
}
private func populateNotes() {
// get the notes array
fileCoordinator.coordinateReadingItemAtURL(presentedItemURL!, options: nil, error: nil) { (newURL :NSURL!) -> Void in
let savedData = NSData(contentsOfURL: newURL)
if savedData == nil {
self.notes = NSMutableArray()
}
else {
self.notes = NSKeyedUnarchiver.unarchiveObjectWithData(savedData!) as NSMutableArray
}
}
}
private func saveNote(note :String) {
// write note into the notes array
fileCoordinator.coordinateWritingItemAtURL(presentedItemURL!, options: nil, error: nil) { ( newURL :NSURL!) -> Void in
self.notes.addObject(note)
let saveData = NSKeyedArchiver.archivedDataWithRootObject(self.notes)
let success = saveData.writeToURL(newURL, atomically: true)
}
}
private func updateNotes() {
// write note into the notes array
fileCoordinator.coordinateWritingItemAtURL(presentedItemURL!, options: nil, error: nil) { ( newURL :NSURL!) -> Void in
let saveData = NSKeyedArchiver.archivedDataWithRootObject(self.notes)
let success = saveData.writeToURL(newURL, atomically: true)
}
}
private func deleteAllNotes() {
fileCoordinator.coordinateWritingItemAtURL(presentedItemURL!, options: nil, error: nil) { ( newURL :NSURL!) -> Void in
self.notes = NSMutableArray() // writing an empty array
let saveData = NSKeyedArchiver.archivedDataWithRootObject(self.notes)
let success = saveData.writeToURL(newURL, atomically: true)
}
}
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 Potentially incomplete method implementation.
// Return the number of sections.
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
return self.notes.count
}
override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 44.0
}
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView(frame: CGRectMake(0, 0, self.view.bounds.width, 44))
headerView.layer.borderWidth = 0.5
headerView.layer.borderColor = UIColor.lightGrayColor().CGColor
let newNoteTextField = UITextField(frame: CGRectMake(10, 0, self.view.bounds.width, 44))
newNoteTextField.placeholder = "Enter new note"
newNoteTextField.delegate = self
newNoteTextField.backgroundColor = UIColor.whiteColor()
newNoteTextField.returnKeyType = UIReturnKeyType.Done
headerView.addSubview(newNoteTextField)
return headerView
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("NoteCell", forIndexPath: indexPath) as UITableViewCell
var note = self.notes[indexPath.row] as String
cell.textLabel?.text = note
return cell
}
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true;
}
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == UITableViewCellEditingStyle.Delete {
self.notes.removeObjectAtIndex(indexPath.row)
updateNotes()
self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
}
}
func textFieldDidEndEditing(textField: UITextField) {
saveNote(textField.text)
// reload table
self.tableView.reloadData()
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
return textField.resignFirstResponder()
}
}