-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntryListTableViewController.swift
More file actions
112 lines (96 loc) · 4.71 KB
/
EntryListTableViewController.swift
File metadata and controls
112 lines (96 loc) · 4.71 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
//
// EntryListTableViewController.swift
// DiaryApp
//
// Created by Michael Flowers on 1/28/20.
// Copyright © 2020 Michael Flowers. All rights reserved.
//
import UIKit
import CoreData
class EntryListTableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
EntryController.shared.fetchedResultsController.delegate = self
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return EntryController.shared.fetchedResultsController.sections?.count ?? 0
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return EntryController.shared.fetchedResultsController.sections?[section].numberOfObjects ?? 0
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "entryCell", for: indexPath)
let entry = EntryController.shared.fetchedResultsController.object(at: indexPath)
cell.textLabel?.text = entry.name
cell.detailTextLabel?.text = entry.timestamp?.prettyDate()
return cell
}
// Override to support editing the table view.
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
let entry = EntryController.shared.fetchedResultsController.object(at: indexPath)
EntryController.shared.delete(entry: entry)
}
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return EntryController.shared.fetchedResultsController.sections?[section].name
}
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "cellSegue" {
guard let destinationVC = segue.destination as? DetailEntryViewController, let indexPath = tableView.indexPathForSelectedRow else {
print("Error in file: \(#file), in the body of the function: \(#function) on line: \(#line)\n")
return
}
let entry = EntryController.shared.fetchedResultsController.object(at: indexPath)
destinationVC.entry = entry
}
}
}
//MARK: - NSFetchedResultsControllerDelegate Methods
extension EntryListTableViewController: NSFetchedResultsControllerDelegate {
//will tell the tableViewController get ready to do something.
func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
tableView.beginUpdates()
}
func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
tableView.endUpdates()
}
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>,
didChange anObject: Any,
at indexPath: IndexPath?,
for type: NSFetchedResultsChangeType,
newIndexPath: IndexPath?) {
switch type {
case .insert:
//there was a new entry so now we need to make a new cell.
guard let newIndexPath = newIndexPath else {return}
tableView.insertRows(at: [newIndexPath], with: .automatic)
case .delete:
guard let indexPath = indexPath else {return}
tableView.deleteRows(at: [indexPath], with: .fade)
case .move:
guard let indexPath = indexPath, let newIndexpath = newIndexPath else {return}
tableView.moveRow(at: indexPath, to: newIndexpath)
case .update:
guard let indexPath = indexPath else {return}
tableView.reloadRows(at: [indexPath], with: .automatic)
@unknown default:
fatalError()
}
}
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange sectionInfo: NSFetchedResultsSectionInfo, atSectionIndex sectionIndex: Int, for type: NSFetchedResultsChangeType) {
switch type {
case .insert:
let indexSet = IndexSet(integer: sectionIndex)
tableView.insertSections(indexSet, with: .automatic)
case .delete:
let indexSSet = IndexSet(integer: sectionIndex)
tableView.deleteSections(indexSSet, with: .automatic)
default:
break
}
}
}