我有一个连接到表格视图的搜索栏。当搜索栏未激活但在搜索栏处于活动状态时禁用单元格选择时,它完全按照我的意愿工作。我调试了它,当搜索处于活动状态时选择一行时,甚至没有调用didSelectRowAtIndexPath。可能导致这种情况的原因是什么?
以下是相关代码:
class FabricsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate, UISearchControllerDelegate, UISearchDisplayDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
searchController.delegate = self
searchController.searchBar.delegate = self
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
searchController.definesPresentationContext = false
searchController.hidesNavigationBarDuringPresentation = false
myTableView.tableHeaderView = searchController.searchBar
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if searching {
searching = false
searchBar?.resignFirstResponder()
FirebaseClient.sharedInstance.joinFabric(uid: self.appDelegate.uid!, fabricKey: allFabrics[indexPath.row].key)
updateFabricList()
} else {
appDelegate.selectedFabricKey = joinedFabrics[indexPath.row].key
performSegue(withIdentifier: "fabricSelected", sender: self)
}
myTableView.deselectRow(at: indexPath, animated: false)
}
func filterContentForSearchText(searchText: String, scope: String = "All") {
if let allFabrics = allFabrics {
filteredFabrics = allFabrics.filter { fabric in
return (fabric.name.lowercased().contains(searchText.lowercased()))
}
myTableView.reloadData()
myTableView.setContentOffset(CGPoint.zero, animated: false)
}
}
}
extension FabricsViewController: UISearchResultsUpdating {
func updateSearchResults(for: UISearchController) {
filterContentForSearchText(searchText: searchController.searchBar.text!)
}
}
答案 0 :(得分:0)
覆盖func tableView(tableView:UITableView,didSelectRowAtIndexPath indexPath:NSIndexPath){
答案 1 :(得分:0)
对于面临相同问题的用户,根据Apple的文档,您应该具有以下代码来添加searchController:
if #available(iOS 11.0, *) {
navigationItem.searchController = searchController
} else {
self.yourTableView.tableHeaderView = searchController.searchBar
}