-
Notifications
You must be signed in to change notification settings - Fork 0
/
BusinessesViewController.swift
executable file
·207 lines (172 loc) · 7.5 KB
/
BusinessesViewController.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
//
// BusinessesViewController.swift
// Yelp
//
// Created by Timothy Lee on 4/23/15.
// Copyright (c) 2015 Timothy Lee. All rights reserved.
//
import UIKit
class BusinessesViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate, UIScrollViewDelegate {
var businesses: [Business]!
var allBusinesses: [Business]!
var filteredBusinesses: [Business]!
var isMoreDataLoading = false
@IBOutlet weak var searchBar: UISearchBar!
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
searchBar.delegate = self
//searchBar = UISearchBar()
//searchBar.sizeToFit()
navigationItem.titleView = searchBar
tableView.dataSource = self
tableView.delegate = self
scrollView.delegate = self
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 120
let navigationBar = navigationController?.navigationBar
navigationBar?.backgroundColor = UIColor(red: 1.0, green: 0, blue: 0, alpha: 1.0)
Business.searchWithTerm("Thai", completion: { (businesses: [Business]!, error: NSError!) -> Void in
self.businesses = businesses
self.tableView.reloadData()
self.allBusinesses = businesses
for business in businesses {
print(business.name!)
print(business.address!)
}
})
/* //Example of Yelp search with more search options specified
Business.searchWithTerm("Restaurants", sort: .Distance, categories: ["asianfusion", "burgers"], deals: true) { (businesses: [Business]!, error: NSError!) -> Void in
self.businesses = businesses
for business in businesses {
print(business.name!)
print(business.address!)
}
}
*/
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
if businesses != nil
{
return businesses!.count
}
else
{
return 0
}
}
// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("BusinessCell", forIndexPath: indexPath) as! BusinessCell
cell.business = businesses[indexPath.row]
return cell
}
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
// When there is no text, filteredData is the same as the original data
businesses = allBusinesses
if searchText.isEmpty {
businesses = allBusinesses
} else {
// The user has entered text into the search box
// Use the filter method to iterate over all items in the data array
// For each item, return true if the item should be included and false if the
// item should NOT be included
filteredBusinesses = businesses!.filter({(dataItem: Business) -> Bool in
let businessName = dataItem.name
if businessName!.rangeOfString(searchText, options: .CaseInsensitiveSearch) != nil {
print("true")
return true
} else {
print("false")
return false
}
})
}
businesses = filteredBusinesses
tableView.reloadData()
print("searchbar updated")
}
func searchBarCancelButtonClicked(searchBar: UISearchBar) {
searchBar.text = ""
print("searchbar canceled")
businesses = allBusinesses
searchBar.resignFirstResponder()
self.tableView.reloadData()
}
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
searchBar.resignFirstResponder()
}
/*func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
// When there is no text, filteredData is the same as the original data
if searchText.isEmpty {
businesses = allBusinesses
} else {
// The user has entered text into the search box
// Use the filter method to iterate over all items in the data array
// For each item, return true if the item should be included and false if the
// item should NOT be included
filteredBusinesses = businesses.filter({(dataItem: Business) -> Bool in
let businessName = dataItem.name
if businessName!.rangeOfString(searchText, options: .CaseInsensitiveSearch) != nil {
return true
} else {
return false
}
})
}
tableView.reloadData()
}*/
func scrollViewDidScroll(scrollView: UIScrollView) {
//print("scrolling")
if (!isMoreDataLoading) {
// Calculate the position of one screen length before the bottom of the results
let scrollViewContentHeight = tableView.contentSize.height
let scrollOffsetThreshold = scrollViewContentHeight - tableView.bounds.size.height
//print("more data loading")
// When the user has scrolled past the threshold, start requesting
if(scrollView.contentOffset.y > scrollOffsetThreshold && tableView.dragging) {
isMoreDataLoading = true
print("would load more data")
// ... Code to load more results ...
loadMoreData()
}
}
}
func loadMoreData()
{
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let test = sender as? UITableViewCell
{
let cell = sender as! UITableViewCell
var indexPath = tableView.indexPathForCell(sender as! UITableViewCell)
let business = businesses![indexPath!.row]
let detailViewController = segue.destinationViewController as! DetailViewController
detailViewController.business = business
cell.selectionStyle = .None
//let backgroundView = UIView()
//backgroundView.backgroundColor = UIColor.redColor()
//cell.selectedBackgroundView = backgroundView
tableView.deselectRowAtIndexPath(indexPath!, animated:true)
print("prepare for segue")
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
}
}