我有一个带有MapView和ScrollView的ViewController A和一个搜索按钮。如果用户点击此按钮,搜索器将出现在searchController中。我希望搜索栏在用户开始输入时显示一个新的TableView(称为TableSearchController)。我试图实现,但我没有运气处理这个问题。我真的很感激可以提供任何帮助。 我无法在Google,Youtube或此处找到任何解决方案,但我看到了一些问题,但答案无效:How to show tableview when user taps searchbar swift和Display table view when searchBar (from searchController) begin edited Swift以及How to make UISearchController's searchResultsController display below the searchbar
我该怎么做?帮助
我的ViewController A代码:
var tableSearchController: TableSearchController!
@IBAction func searchUserAction(_ sender: AnyObject) {
let searchController = UISearchController(searchResultsController: tableSearchController)
searchController.searchBar.delegate = self
self.present(searchController, animated: true, completion: nil)
}
我的TableSearchController应该在SearchBar中的开始类型后显示:
override func viewDidLoad() {
super.viewDidLoad()
self.resultsController.tableView.dataSource = self
self.resultsController.tableView.delegate = self
Database.database().reference().child("users").queryOrdered(byChild: "pseudo").observeSingleEvent(of: .value, with: { (snapshot) in
[...] // Here I retrieve from Firebase database the nickname of users and put it in an array
let nickname = userDict["nickname"] as! String // Find username in Firebase
self.userTab.append(nickname) // And put all username in an array
self.resultsController.tableView.reloadData()
})
self.searchController.searchResultsUpdater = self
self.definesPresentationContext = true
}
func updateSearchResults(for searchController: UISearchController) {
self.filteredUsers = self.userTab.filter { (user:String) -> Bool in
if user.lowercased().contains(self.searchController.searchBar.text!.lowercased()) {
return true
} else {
return false
}
}
self.resultsController.tableView.reloadData()
}