-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMainViewController.swift
138 lines (95 loc) · 4.81 KB
/
MainViewController.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
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
//
// MainViewController.swift
// MunchkinCompanion
//
// Created by Alex Gaesser on 1/31/15.
// Copyright (c) 2015 AlexGaesser. All rights reserved.
//
import UIKit
import CoreData
class MainViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, NSFetchedResultsControllerDelegate {
@IBOutlet weak var tableView: UITableView!
let managedObjectContext = (UIApplication.sharedApplication().delegate as AppDelegate).managedObjectContext!
var fetchedResultsController:NSFetchedResultsController = NSFetchedResultsController()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
fetchedResultsController = getFetchResultsController()
fetchedResultsController.delegate = self
fetchedResultsController.performFetch(nil)
println("viewDidLoad called")
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showUserDetailVC" {
let userDetailVC: UserDetailViewController = segue.destinationViewController as UserDetailViewController
let indexPath = self.tableView.indexPathForSelectedRow()
let thisUser = fetchedResultsController.objectAtIndexPath(indexPath!) as UserModel
userDetailVC.detailUserModel = thisUser
} else if segue.identifier == "showAddUserVC" {
let addUserVC:AddUserViewController = segue.destinationViewController as AddUserViewController
}
}
@IBAction func addUserBarButtonItemPressed(sender: UIBarButtonItem) {
self.performSegueWithIdentifier("showAddUserVC", sender: self)
}
// UITableViewDataSource
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return fetchedResultsController.sections!.count
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return fetchedResultsController.sections![section].numberOfObjects
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
println("cellForRowAtIndexPath called")
let thisUser = fetchedResultsController.objectAtIndexPath(indexPath) as UserModel
var cell: UserCell = tableView.dequeueReusableCellWithIdentifier("userCell") as UserCell
cell.nameLabel.text = thisUser.userName
cell.levelLabel.text = "\(thisUser.level)"
cell.combatLabel.text = "\(thisUser.effectiveCombat)"
println("returning cell")
return cell
}
// UITableViewDelegate
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
println(indexPath.row)
self.performSegueWithIdentifier("showUserDetailVC", sender: self)
}
func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if (editingStyle == UITableViewCellEditingStyle.Delete) {
let appDel:AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let context:NSManagedObjectContext = appDel.managedObjectContext!
let thisUser = fetchedResultsController.objectAtIndexPath(indexPath) as UserModel
context.deleteObject(thisUser)
context.save(nil)
}
}
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 25
}
// NSFetchedResultControllerDelegate
func controllerDidChangeContent(controller: NSFetchedResultsController) {
tableView.reloadData()
println("DidChangeContent called")
}
// Helper functions
func userFetchRequest() -> NSFetchRequest {
let fetchRequest = NSFetchRequest(entityName: "UserModel")
let sortDescriptor = NSSortDescriptor(key: "level", ascending: false)
fetchRequest.sortDescriptors = [sortDescriptor]
return fetchRequest
}
func getFetchResultsController() -> NSFetchedResultsController {
fetchedResultsController = NSFetchedResultsController(fetchRequest: userFetchRequest(), managedObjectContext: managedObjectContext, sectionNameKeyPath: nil, cacheName: nil)
return fetchedResultsController
}
}