我的 UISearchBar 的实现有问题。它没有更新我的 tableView 中的结果,我不知道为什么它不起作用。 Here是我类(class)的完整源代码。
var searchBar = UISearchBar()
func updateSearchResults(for searchController: UISearchBar) {
searchBarText = searchController.text?.lowercased()
searchBarScope = searchController.selectedScopeButtonIndex
showList()
}
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
self.searchBar.showsScopeBar = true
if (navigationItem.title != nil) {
self.searchBar.scopeButtonTitles = [String(format: "gesamte %@", ci("project_s")), String(format: "in %@", navigationItem.title!)]
}
}
func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
updateSearchResults(for: searchBar)
}
override func viewDidLayoutSubviews() {
self.searchBar.sizeToFit()
}
public override func viewDidLoad() {
self.navigationController?.setNavigationBarHidden(false, animated: false)
navigationItem.title = navigationItem.title ?? ci("plan_p")
guard let projectId = GlobalState.selectedProjectId, let byProject : Results<Structure> = self.by(projectId: projectId) else {
return
}
//search bar
tableView.rowHeight = 100.0
tableView.tableHeaderView = searchBar
self.searchBar.showsCancelButton = true
self.searchBar.sizeToFit()
self.definesPresentationContext = true
self.searchBar.delegate = self
tableView.allowsMultipleSelectionDuringEditing = true
我错过了什么?感谢你们的任何意见!
最佳答案
在 viewDidLoad 中设置 tableView 委托(delegate)和数据源,如下所示:
tableView.delegate = self;
table.dataSource = self;
我认为你上面遗漏了。
关于swift - 为什么我的 UISearchBar 没有更新结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57905586/