当搜索栏处于活动状态时无法选择TableViewCell - iOS Swift

时间:2016-12-23 19:50:52

标签: ios tableview searchbar

我有一个连接到表格视图的搜索栏。当搜索栏未激活但在搜索栏处于活动状态时禁用单元格选择时,它完全按照我的意愿工作。我调试了它,当搜索处于活动状态时选择一行时,甚至没有调用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!)
    }
}

2 个答案:

答案 0 :(得分:0)

  1. 请检查方法名称的文字。 确保您使用的方法名称如下:
  2. 覆盖func tableView(tableView:UITableView,didSelectRowAtIndexPath indexPath:NSIndexPath){

    1. 您已将searchBar的委托设置为self。您是否在搜索变为活动状态时激活任何点按手势?此点击手势可以中断您的didSelectRowAtIndexPath方法。

答案 1 :(得分:0)

对于面临相同问题的用户,根据Apple的文档,您应该具有以下代码来添加searchController:

if #available(iOS 11.0, *) {
   navigationItem.searchController = searchController
} else {
    self.yourTableView.tableHeaderView = searchController.searchBar
}