-
Notifications
You must be signed in to change notification settings - Fork 0
/
FetchedResultsTableViewController.swift
106 lines (84 loc) · 3.79 KB
/
FetchedResultsTableViewController.swift
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
//
// FetchedResultsTableViewController.swift
// Planbok
//
// Created by Jaime on 10/22/14.
// Copyright (c) 2014 Jaime Zaragoza. All rights reserved.
//
import UIKit
import CoreData
class FetchedResultsTableViewController: UITableViewController, NSFetchedResultsControllerDelegate {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
var changeIsUserDriven: Bool = false
// MARK: Fetching
var fetchedResultsController: NSFetchedResultsController? {
didSet {
if oldValue != fetchedResultsController {
fetchedResultsController?.delegate = self
performFetch()
}
}
}
func performFetch() {
do {
try fetchedResultsController!.performFetch()
} catch let error as NSError {
print("performFetch: failed")
print("CoreData Error: \(error.localizedDescription)")
}
tableView.reloadData()
}
// MARK: - UITableViewDataSource
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return fetchedResultsController!.sections!.count
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let sectionInfo = fetchedResultsController!.sections![section] as NSFetchedResultsSectionInfo
return sectionInfo.numberOfObjects
}
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
let sectionInfo = fetchedResultsController!.sections![section] as NSFetchedResultsSectionInfo
return sectionInfo.name
}
// MARK: NSFetchedResultsControllerDelegate
func controllerWillChangeContent(controller: NSFetchedResultsController) {
if !changeIsUserDriven {
tableView.beginUpdates()
}
}
func controller(controller: NSFetchedResultsController, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType) {
if !changeIsUserDriven {
switch type {
case NSFetchedResultsChangeType.Insert:
tableView.insertSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Fade)
case NSFetchedResultsChangeType.Delete:
tableView.deleteSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Fade)
default:
return
}
}
}
func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
if !changeIsUserDriven {
switch type {
case NSFetchedResultsChangeType.Insert:
tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade)
case NSFetchedResultsChangeType.Delete:
tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
case NSFetchedResultsChangeType.Move:
tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade)
case NSFetchedResultsChangeType.Update:
tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Automatic)
tableView.insertRowsAtIndexPaths([indexPath!], withRowAnimation: .Automatic)
}
}
}
func controllerDidChangeContent(controller: NSFetchedResultsController) {
if !changeIsUserDriven {
tableView.endUpdates()
}
}
}