-
Notifications
You must be signed in to change notification settings - Fork 2
/
CandyTableViewController.swift
122 lines (96 loc) · 4.82 KB
/
CandyTableViewController.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
//
// CandyTableViewController.swift
// 013-UISearchBar
//
// Created by Audrey Li on 4/6/15.
// Copyright (c) 2015 com.shomigo. All rights reserved.
//
import UIKit
private struct Constants {
static let ShowDetailIdentifier = "showDetail"
}
class CandyTableViewController: UITableViewController, UISearchBarDelegate, UISearchDisplayDelegate {
var data = [Candy]()
var filteredData = [Candy]()
override func viewDidLoad() {
super.viewDidLoad()
tableView.estimatedRowHeight = 68.0
tableView.rowHeight = UITableViewAutomaticDimension
self.data = [Candy(category:"Chocolate", name:"chocolate Bar"),
Candy(category:"Chocolate", name:"chocolate Chip"),
Candy(category:"Chocolate", name:"dark chocolate"),
Candy(category:"Hard", name:"lollipop"),
Candy(category:"Hard", name:"candy cane"),
Candy(category:"Hard", name:"jaw breaker"),
Candy(category:"Other", name:"caramel"),
Candy(category:"Other", name:"sour chew"),
Candy(category:"Other", name:"gummi bear")]
self.tableView.reloadData()
}
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
println("text changed \(searchText)")
data.removeRange(0..<4)
self.tableView.reloadData()
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.performSegueWithIdentifier(Constants.ShowDetailIdentifier, sender: tableView)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == Constants.ShowDetailIdentifier {
let candyDetailViewController = segue.destinationViewController as UIViewController
if sender as UITableView == self.searchDisplayController!.searchResultsTableView {
let indexPath = self.searchDisplayController!.searchResultsTableView.indexPathForSelectedRow()!
candyDetailViewController.title = self.filteredData[indexPath.row].name
} else {
let indexPath = self.tableView.indexPathForSelectedRow()!
candyDetailViewController.title = self.data[indexPath.row].name
}
}
}
func filterContentForSearchText(searchText:String){
self.filteredData = self.data.filter({ (candy: Candy)-> Bool in
let stringMatch = candy.name.rangeOfString(searchText)
return stringMatch != nil ? true : false
})
}
func filterContentForSearchTextWithScope(searchText: String, scope: String = "All") {
self.filteredData = self.data.filter({( candy : Candy) -> Bool in
var categoryMatch = (scope == "All") || (candy.category == scope)
var stringMatch = candy.name.rangeOfString(searchText)
return categoryMatch && (stringMatch != nil)
})
}
func searchDisplayController(controller: UISearchDisplayController, shouldReloadTableForSearchScope searchOption: Int) -> Bool {
// self.filterContentForSearchText(self.searchDisplayController!.searchBar.text)
let scope = self.searchDisplayController!.searchBar.scopeButtonTitles as [String]
self.filterContentForSearchTextWithScope(self.searchDisplayController!.searchBar.text, scope: scope[searchOption])
return true
}
func searchDisplayController(controller: UISearchDisplayController, shouldReloadTableForSearchString searchString: String!) -> Bool {
// self.filterContentForSearchText(searchString)
let scopes = self.searchDisplayController!.searchBar.scopeButtonTitles as [String]
let selectedScope = scopes[self.searchDisplayController!.searchBar.selectedScopeButtonIndex] as String
self.filterContentForSearchTextWithScope(searchString, scope: selectedScope)
return true
}
// MARK: - Table view data source
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == self.searchDisplayController!.searchResultsTableView {
return self.filteredData.count
} else {
return data.count
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
var candy: Candy
if tableView == self.searchDisplayController!.searchResultsTableView {
candy = filteredData[indexPath.row]
} else {
candy = self.data[indexPath.row]
}
cell.textLabel!.text = candy.name
cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
return cell
}
}