我正在使用搜索栏为tableview工作。
当搜索文本发生变化时,我会调用tableView.reloadData
来创建有关搜索结果的单元格。
但是,即使我拨打tableView.reloadData
,也不会调用cellForRowAtIndexPath
。
所以我没有关于搜索结果的任何单元格。
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)
}
}
当我搜索时,filteredRestaurants数组会显示有关已过滤研究的信息,但不会出现在搜索结果表视图中,因为未调用cellForRowAtIndexPath。
答案 0 :(得分:2)
当searchController在代码的这一部分处于活动状态时,您将返回0
部分的数量。
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
if searchController.active && searchController.searchBar.text != "" {
return 0
}
return restaurantsData.restaurantTypes.count
}
零部分是一个空表,没有单元格,这就是为什么cellForRowAtIndexPath
没有被调用。
尝试将return 0
替换为return 1
,以便该表格中显示搜索结果的部分。
答案 1 :(得分:-1)
您在numberOfSectionsInTableView函数中有以下代码:
searchController.active && searchController.searchBar.text != ""
应该是(==而不是!=):
searchController.active && searchController.searchBar.text == ""