-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCategoryPickerViewController.swift
107 lines (78 loc) · 3.85 KB
/
CategoryPickerViewController.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
//
// CategoryPickerViewController.swift
// Charades
//
// Created by Jeff on 9/3/15.
// Copyright © 2015 Jeff Berman. All rights reserved.
//
import UIKit
class CategoryPickerViewController: UITableViewController {
struct Category {
let name: String
var selected: Bool
}
var categories = [Category]()
// MARK: - Life cycle methods
override func viewDidLoad() {
super.viewDidLoad()
// Load master category list from model and restore previous selections
let model = CharadesGame()
print("Model loaded into CategoryPickerVC")
var savedCategories = NSUserDefaults.standardUserDefaults().stringArrayForKey("selectedCategories")
if savedCategories == nil || savedCategories?.count == 0 { // Never saved
savedCategories = model.masterCategoryList
}
categories = model.masterCategoryList.map({ Category(name: $0, selected: savedCategories!.contains($0)) })
let background = UIImageView(image: UIImage(named: "title background"))
self.tableView.backgroundView = background
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
navigationController?.setNavigationBarHidden(false, animated: false)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return categories.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("CategoryCell", forIndexPath: indexPath) as! CategoryPickerTableViewCell
cell.titleLabel.text = categories[indexPath.row].name
cell.checkmarkImage.image = categories[indexPath.row].selected ? UIImage(named: "GreenCheckmark") : nil
return cell
}
// MARK: - Table view delegate
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: false)
if let cell = tableView.cellForRowAtIndexPath(indexPath) as? CategoryPickerTableViewCell {
categories[indexPath.row].selected = !categories[indexPath.row].selected
cell.checkmarkImage.image = categories[indexPath.row].selected ? UIImage(named: "GreenCheckmark") : nil
// Must have at least one item selected
if categories.filter({ $0.selected == true }).count > 0 {
navigationItem.rightBarButtonItem?.enabled = true
} else {
navigationItem.rightBarButtonItem?.enabled = false
}
}
}
// MARK: - Navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "PlayGameSegue" {
if let vc = segue.destinationViewController as? CharadesGameViewController {
let selectedCategories = categories.filter({ $0.selected }).map({ $0.name })
vc.selectedCategories = selectedCategories
NSUserDefaults.standardUserDefaults().setObject(selectedCategories, forKey: "selectedCategories")
navigationItem.backBarButtonItem = UIBarButtonItem(title: "Abort game", style: UIBarButtonItemStyle.Plain, target: nil, action: nil)
}
}
}
@IBAction func didUnwindToReplay(unwindSegue: UIStoryboardSegue) {
// Nothing to do here; this is just the target of the Unwind.
}
}