-
Notifications
You must be signed in to change notification settings - Fork 10
/
TaskManagerViewController.swift
executable file
·96 lines (81 loc) · 4.11 KB
/
TaskManagerViewController.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
//
// TaskManagerViewController.swift
// TaskManager
//
// Created by Ravi Shankar on 12/07/14.
// Copyright (c) 2014 Ravi Shankar. All rights reserved.
//
import UIKit
import CoreData
class TaskManagerViewController: UITableViewController, NSFetchedResultsControllerDelegate {
//set up the managedObjectContext
let managedObjectContext = (UIApplication.sharedApplication().delegate as AppDelegate).managedObjectContext
//and fetchedResultController (which is an instance of NSFetchedResultsController)
var fetchedResultController: NSFetchedResultsController = NSFetchedResultsController()
override func viewDidLoad() {
//call the superclass
super.viewDidLoad()
// call getFetchedResultController and change the value of fetchedResultController
fetchedResultController = getFetchedResultController()
//set the delegate to self
fetchedResultController.delegate = self
//perform the fetch
fetchedResultController.performFetch(nil)
}
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if segue.identifier == "edit" {
let cell = sender as UITableViewCell
let indexPath = tableView.indexPathForCell(cell)
let taskController:TaskDetailViewController = segue.destinationViewController as TaskDetailViewController
let task:Tasks = fetchedResultController.objectAtIndexPath(indexPath) as Tasks
taskController.task = task
let task2:Tasks = fetchedResultController.objectAtIndexPath(indexPath) as Tasks
taskController.task2 = task2
}
}
func getFetchedResultController() -> NSFetchedResultsController {
//call to taskFetchRequest
fetchedResultController = NSFetchedResultsController(fetchRequest: taskFetchRequest(), managedObjectContext: managedObjectContext, sectionNameKeyPath: nil, cacheName: nil)
return fetchedResultController
}
//defines the fetch request
func taskFetchRequest() -> NSFetchRequest {
//specify the entity
let fetchRequest = NSFetchRequest(entityName: "Tasks")
//2 sort descriptors
let sortDescriptor = NSSortDescriptor (key: "desc", ascending : true)
let sortDescriptor2 = NSSortDescriptor (key: "desc2",ascending : true)
//let sortDescriptor = NSSortDescriptor(key: "desc", ascending: true)
//add the sort descriptors to the fetch request
fetchRequest.sortDescriptors = [sortDescriptor, sortDescriptor2]
return fetchRequest
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// #pragma mark - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView!) -> Int {
return fetchedResultController.sections.count
}
override func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
return fetchedResultController.sections[section].numberOfObjects
}
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell? {
var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
let task = fetchedResultController.objectAtIndexPath(indexPath) as Tasks
cell.textLabel.text = task.desc
cell.detailTextLabel.text = task.desc2
//let task2 = fetchedResultController.objectAtIndexPath(indexPath) as Tasks
//cell.detailTextLabel.text = task2.desc
return cell
}
override func tableView(tableView: UITableView!, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath!) {
let managedObject:NSManagedObject = fetchedResultController.objectAtIndexPath(indexPath) as NSManagedObject
managedObjectContext.deleteObject(managedObject)
managedObjectContext.save(nil)
}
func controllerDidChangeContent(controller: NSFetchedResultsController!) {
tableView.reloadData()
}
}