-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDisplayTableViewController.swift
More file actions
153 lines (101 loc) · 4.44 KB
/
DisplayTableViewController.swift
File metadata and controls
153 lines (101 loc) · 4.44 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
//
// DisplayTableViewController.swift
// CoreDataExample
//
// Created by Farhan Syed on 4/16/17.
// Copyright © 2017 Farhan Syed. All rights reserved.
//
import UIKit
import CoreData
class DisplayTableViewController: UITableViewController, UISearchBarDelegate {
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
var items: [Item] = []
var selectedIndex: Int!
var filteredData: [Item] = []
override func viewDidLoad() {
super.viewDidLoad()
createSearchBar()
self.tableView.estimatedRowHeight = 30
self.tableView.rowHeight = UITableViewAutomaticDimension
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillAppear(_ animated: Bool) {
fetchData()
}
func fetchData() {
do {
items = try context.fetch(Item.fetchRequest())
filteredData = items
DispatchQueue.main.async {
self.tableView.reloadData()
}
} catch {
print("Couldn't Fetch Data")
}
}
}
extension DisplayTableViewController {
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as UITableViewCell
let date = items.reversed()[indexPath.row].date
let time = items.reversed()[indexPath.row].time
cell.textLabel?.text = items.reversed()[indexPath.row].name
if let date = date, let time = time {
let timeStamp = "Added on \(date) at \(time)"
cell.detailTextLabel?.text = timeStamp
}
return cell
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return filteredData.count
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
selectedIndex = indexPath.row
performSegue(withIdentifier: "UpdateVC", sender: self)
tableView.deselectRow(at: indexPath, animated: true)
}
override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let delete = UITableViewRowAction(style: .default, title: "Delete") { (action, indexPath) in
// delete item at indexPath
let item = self.filteredData[indexPath.row]
self.context.delete(item)
(UIApplication.shared.delegate as! AppDelegate).saveContext()
self.filteredData.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
}
let share = UITableViewRowAction(style: .default, title: "Share") { (action, indexPath) in
// delete item at indexPath
print("Share")
}
delete.backgroundColor = UIColor(red: 224/255, green: 47/255, blue: 55/255, alpha: 1.0)
share.backgroundColor = UIColor(red: 47/255, green: 182/255, blue: 224/255, alpha: 1.0)
return [delete,share]
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "UpdateVC" {
let updateVC = segue.destination as! UpdateItemViewController
updateVC.item = filteredData[selectedIndex!]
}
}
func createSearchBar() {
let searchBar = UISearchBar()
searchBar.showsCancelButton = false
searchBar.placeholder = "Search"
searchBar.delegate = self
self.navigationItem.titleView = searchBar
}
// This method updates filteredData based on the text in the Search Box
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
if searchText.isEmpty {
filteredData = items
} else {
filteredData = items.filter { ($0.name?.lowercased().contains(searchText.lowercased()))! }
}
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}