Skip to content

Commit

Permalink
[NEW FEATURE] ✨ Add search functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Williano committed Mar 9, 2019
1 parent 60d9c21 commit dbc9c1a
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 12 deletions.
Binary file not shown.
69 changes: 57 additions & 12 deletions FoodPin/RestaurantTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ import CoreData
class RestaurantTableViewController: UITableViewController {

var restaurants:[RestaurantMO] = []

var fetchResultController: NSFetchedResultsController<RestaurantMO>!
var searchController: UISearchController!
var searchResults: [RestaurantMO] = []

override func viewDidLoad() {
super.viewDidLoad()
Expand All @@ -24,6 +25,12 @@ class RestaurantTableViewController: UITableViewController {
tableView.estimatedRowHeight = 80.0
// tableView.rowHeight = UITableView.automaticDimension

searchController = UISearchController(searchResultsController: nil)
tableView.tableHeaderView = searchController.searchBar
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search restaurants..."

// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false

Expand All @@ -48,7 +55,9 @@ class RestaurantTableViewController: UITableViewController {
} catch {
print(error)
}
} }
}

}

// override func viewWillAppear(_ animated: Bool) {
// super.viewWillAppear(animated)
Expand All @@ -64,34 +73,45 @@ class RestaurantTableViewController: UITableViewController {

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return restaurants.count
if searchController.isActive {
return searchResults.count
} else {
return restaurants.count
}
}


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "Cell"
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! RestaurantTableViewCell

// Determine if we get the restaurant from search result or the original array
let restaurant = (searchController.isActive) ? searchResults[indexPath.row] : restaurants[indexPath.row]

// Configure the cell...
cell.nameLabel.text = restaurants[indexPath.row].name
cell.locationLabel.text = restaurants[indexPath.row].location
cell.typeLabel.text = restaurants[indexPath.row].type
if let restaurantImage = restaurants[indexPath.row].image {
cell.nameLabel.text = restaurant.name
cell.locationLabel.text = restaurant.location
cell.typeLabel.text = restaurant.type
if let restaurantImage = restaurant.image {
cell.thumbnailImageView.image = UIImage(data: restaurantImage as Data)
}
cell.accessoryType = restaurants[indexPath.row].isVisited ? .checkmark : .none
cell.accessoryType = restaurant.isVisited ? .checkmark : .none

return cell
}


/*

// Override to support conditional editing of the table view.
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
// Return false if you do not want the specified item to be editable.
return true
if searchController.isActive {
return false
} else {
return true
}
}
*/


override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
// Social Sharing Button
Expand Down Expand Up @@ -196,13 +216,25 @@ class RestaurantTableViewController: UITableViewController {
guard let indexPath = tableView.indexPathForSelectedRow else {return}
let destinationController = segue.destination as! RestaurantDetailViewController
// Pass the selected object to the new view controller.
destinationController.restaurant = restaurants[indexPath.row]
destinationController.restaurant = (searchController.isActive) ? searchResults[indexPath.row] : restaurants[indexPath.row]
}
}

@IBAction func unwindToHomeScreen(segue: UIStoryboardSegue) {

}

// MARK: - Search
func filterContent(for searchText: String) {
searchResults = restaurants.filter({ (restaurant) -> Bool in
if let name = restaurant.name, let location = restaurant.location, let type = restaurant.type {
let isMatch = name.localizedCaseInsensitiveContains(searchText) || location.localizedCaseInsensitiveContains(searchText) || type.localizedCaseInsensitiveContains(searchText)
return isMatch
}

return false
})
}

}

Expand Down Expand Up @@ -243,3 +275,16 @@ extension RestaurantTableViewController: NSFetchedResultsControllerDelegate {
}

}


extension RestaurantTableViewController: UISearchResultsUpdating {

func updateSearchResults(for searchController: UISearchController) {
if let searchText = searchController.searchBar.text {
filterContent(for: searchText)
tableView.reloadData()
}
}


}

0 comments on commit dbc9c1a

Please sign in to comment.