我花了很多时间才找到解决方案,但我没找到,所以我在这里请求帮助。
我有一个带按钮的ViewController,当我按下这个按钮时,searchController会出现一个searchBar,当我开始在searchBar中输入一个字母时,tableViewController的tableView出现在主视图上方和searchController下方。但是tableView的单元格上没有出现结果,因为没有调用cellForRowAt indexPath方法。
以下代码适用于ViewController:
class ViewController: UIViewController, UISearchControllerDelegate, UISearchBarDelegate {
var tableViewSearch: TableViewController!
lazy var searchController: UISearchController = {
let sc = UISearchController(searchResultsController: self.tableViewSearch)
sc.searchResultsUpdater = self.tableViewSearch
sc.delegate = self
sc.searchBar.delegate = self
return sc
}()
override func viewDidAppear(_ animated: Bool) {
tableViewSearch = TableViewController()
}
@IBAction func searchWithAddress(_ sender: AnyObject) {
self.present(searchController, animated: true, completion: nil)
}
}
以下代码是TableViewController:
class TableViewController: UITableViewController, UISearchResultsUpdating {
var userTab = [String]()
var filteredUsers = [String]()
var searchController: UISearchController!
var resultsController = UITableViewController()
var name: String!
var ref: DatabaseReference!
override func viewDidLoad() {
super.viewDidLoad()
self.resultsController.tableView.delegate = self
self.resultsController.tableView.dataSource = self
ref = Database.database().reference()
ref.child("users").queryOrdered(byChild: "pseudo").observeSingleEvent(of: .value, with: { (snapshot) in
self.userTab = []
for child in snapshot.children {
let userSnap = child as! DataSnapshot
let uid = userSnap.key
let userDict = userSnap.value as! [String: AnyObject]
let pseudo = userDict["pseudo"] as! String
self.userTab.append(pseudo)
self.resultsController.tableView.reloadData()
continue
}
}
})
}
func updateSearchResults(for searchController: UISearchController) {
print("updateSearchResults")
self.filteredUsers = self.userTab.filter { (user:String) -> Bool in
if user.lowercased().contains(searchController.searchBar.text!.lowercased()) {
return true
} else {
return false
}
}
self.resultsController.tableView.reloadData()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
print("prepare")
if segue.identifier == "userProfil" {
let userViewController = segue.destination as! UserViewController
userViewController.pseudo = self.name!
}
}
// Number of rows isn't equal to 0
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == self.tableView {
return self.userTab.count
} else {
return self.filteredUsers.count
}
}
// This method is never called
override func tableView(_ tableView: UITableView!, cellForRowAt indexPath: IndexPath!) -> UITableViewCell {
print("cellForRowAt") // This line is never printed on the console
let cell = UITableViewCell()
if tableView == self.tableView {
cell.textLabel?.text = self.userTab[indexPath.row]
} else {
cell.textLabel?.text = self.filteredUsers[indexPath.row]
}
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let post = filteredUsers[indexPath.row] as! String
self.name = post
performSegue(withIdentifier: "userProfil", sender: nil)
}
}
为什么不调用cellForRowAt indexPath方法?我的代码有什么问题?