Reputation: 171
I'm working for the tableview with search bar.
When the search text is changed, I call tableView.reloadData
to make cells about the search result.
However, even I call tableView.reloadData
, cellForRowAtIndexPath
is not called.
So I don't got any cells about the search result.
import UIKit
class RestaurantsViewController: UITableViewController,UISearchBarDelegate, UISearchResultsUpdating {
@IBOutlet weak var menuButton: UIBarButtonItem!
var restaurantsData = RestaurantsData()
override func viewDidLoad() {
super.viewDidLoad()
if self.revealViewController() != nil {
menuButton.target = self.revealViewController()
menuButton.action = #selector(SWRevealViewController.revealToggle(_:))
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
}
loadRestaurantsData()
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
tableView.tableHeaderView = searchController.searchBar
}
private func loadRestaurantsData() {
let 굽네치킨 = Restaurant(name: "굽네치킨", contact: "051-9196-0940", menus: ["고추바사삭":"20000원"])
let 천궁반점 = Restaurant(name: "천궁반점", contact: "051-2254-0940", menus: ["간짜장":"5000원"])
restaurantsData.addRestaurant("치킨", restaurant: 굽네치킨)
restaurantsData.addRestaurant("중식", restaurant: 천궁반점)
}
// MARK: - UITableViewDelegates
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
if searchController.active && searchController.searchBar.text != "" {
return 0
}
return restaurantsData.restaurantTypes.count
}
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return restaurantsData.restaurantTypes[section]
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if searchController.active && searchController.searchBar.text != "" {
return filteredRestaurants.count
}
return restaurantsData.restaurants[section].count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
let restaurant: Restaurant
print("\(searchController.active), \(self.searchController.searchBar.text != "")")
print(searchController.searchBar.text)
print(searchText)
print(2)
if searchController.active && searchText != "" {
restaurant = filteredRestaurants[indexPath.row]
print("searched")
} else {
restaurant = restaurantsData.restaurants[indexPath.section][indexPath.row]
}
cell.textLabel?.text = restaurant.name
return cell
}
// MARK: - UISearchBarDelegates
let searchController = UISearchController(searchResultsController: nil)
var searchText: String = ""
var filteredRestaurants = [Restaurant]() {
didSet { print(filteredRestaurants) }
}
func filterContentForSearchText(searchText: String, scope: String = "All") {
filteredRestaurants = restaurantsData.allRestaurants.filter { restaurant in
return restaurant.info.containsString(searchText.lowercaseString)
}
print("REloaded")
self.tableView.reloadData()
print(3)
}
func updateSearchResultsForSearchController(searchController: UISearchController) {
filterContentForSearchText(searchController.searchBar.text!)
searchText = searchController.searchBar.text!
print(1)
}
}
When I search, the filteredRestaurants array gots info about filtered research, however dose not present on search result table view because cellForRowAtIndexPath is not called.
Upvotes: 0
Views: 250
Reputation: 754
You have the following code in numberOfSectionsInTableView function:
searchController.active && searchController.searchBar.text != ""
It should be (== instead of !=):
searchController.active && searchController.searchBar.text == ""
Upvotes: -1
Reputation: 3760
You're returning 0
for number of sections when the searchController is active in this part of your code.
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
if searchController.active && searchController.searchBar.text != "" {
return 0
}
return restaurantsData.restaurantTypes.count
}
Zero sections is an empty table with no cells thats why cellForRowAtIndexPath
is not being called.
Try replacing return 0
with return 1
so the table has a section to display your search results.
Upvotes: 2