-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotesTableViewController.swift
More file actions
195 lines (138 loc) · 6.89 KB
/
NotesTableViewController.swift
File metadata and controls
195 lines (138 loc) · 6.89 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
//
// NotesTableViewController.swift
// Notes_Final_Project
//
// Created by abrahim abrahao on 2016-11-25.
// Copyright © 2016 abrahim abrahao. All rights reserved.
//
import UIKit
import CoreData
class NotesTableViewController: UITableViewController, NSFetchedResultsControllerDelegate, UISearchBarDelegate, UISearchDisplayDelegate {
// var SubjectArray = ["School", "Work", "Todo","Appointment", "Task", "Doctor","Shopping", "Reminder", "Others"]
@IBOutlet weak var searchBarNotes: UISearchBar!
@IBOutlet var tableViewNotes: UITableView!
var notestList = [NSManagedObject]()
let managedObjectContext = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
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()
let entityDescription =
NSEntityDescription.entity(forEntityName: "MyNotes",
in: managedObjectContext)
let request: NSFetchRequest<MyNotes> = MyNotes.fetchRequest()
request.entity = entityDescription
do {
let results = try managedObjectContext.fetch(request as! NSFetchRequest<NSFetchRequestResult>)
if results.count > 0 {
notestList = results as! [NSManagedObject]
}
} catch let error {
print(error.localizedDescription)
}
tableViewNotes.delegate = self
tableViewNotes.dataSource = self
searchBarNotes.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return notestList.count//rowsOfSection(sec: section.description)
}
// func numberOfSectionsInTableView(tableView: UITableView) -> Int
// {
// return notestList.count
// }
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellNotes", for: indexPath)
cell.textLabel?.text = notestList[indexPath.row].value(forKey: "event_name") as? String
let dateCell = notestList[indexPath.row].value(forKey: "date_time") as? Date
cell.detailTextLabel?.text = dateCell?.description //notestList[indexPath.row].value(forKey: "data_time") as? String
let img: NSData? = notestList[indexPath.row].value(forKey: "photo") as? NSData
if ((img) != nil) {
cell.imageView?.image = UIImage(data: img! as Data)
} else {
cell.imageView?.image = #imageLiteral(resourceName: "no-image-icon")
}
return cell
//let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as CustomCell
//cell.name.text = table_data[indexPath.section].data[indexPath.row]
//cell.index.text = String(indexPath.row)
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let saveNotes = self.storyboard?.instantiateViewController(withIdentifier: "ViewController") as! ViewController
saveNotes.nowNotes = [notestList[indexPath.row]]
self.present(saveNotes, animated: true, completion: nil)
//self.performSegue(withIdentifier: "SegueAdd", sender: indexPath.row)
}
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
if editingStyle == .delete {
let notes = notestList[indexPath.row]
context.delete(notes)
(UIApplication.shared.delegate as! AppDelegate).saveContext()
do {
notestList = try context.fetch(MyNotes.fetchRequest())
tableView.deleteRows(at: [indexPath], with: .fade)
}
catch {
print("Feching Failed")
}
} else if editingStyle == .insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
// filter
/*
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
searchActive = true;
}
func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
searchActive = false;
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
searchActive = false;
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
searchActive = false;
}*/
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
let request: NSFetchRequest<MyNotes> = MyNotes.fetchRequest() //NSFetchRequest<NSFetchRequestResult>(entityName: "MyNotes")
if (searchText.characters.count > 0 && searchText.characters.isEmpty != true && searchText != " " ) {
let predicate = NSPredicate(format: "event_name CONTAINS[c] '" + searchText + "'")
request.predicate = predicate
}
do {
let results = try managedObjectContext.fetch(request as! NSFetchRequest<NSFetchRequestResult>)
// if results.count > 0 {
notestList = results as! [NSManagedObject]
// }
} catch let error {
print(error.localizedDescription)
notestList = [NSManagedObject]()
}
/*
if(filtered.count == 0){
searchActive = false;
} else {
searchActive = true;
}*/
self.tableView.reloadData()
}
// --------------- My Functions
func msgAlert(msg: String) {
let alert = UIAlertController(title: "Alert", message: msg, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}