首页 > 解决方案 > 如何在使用 Swift 单击 UITableview 单元格之前检查 SearchController 是否处于活动状态

问题描述

我正在SearchControllertableview数据实现。在这里,在单击搜索结果单元格以移动另一个详细信息页面之后搜索特定数据。现在,如果我回到主视图控制器搜索控制器显示而不解雇。我尝试了下面的代码它在主视图控制器场景的细节上工作正常,但是如果我单击没有搜索文本的单元格,那么它会崩溃。如何解决?

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

  let section = isFiltering ? filteredSections[indexPath.section] : sections[indexPath.section] // without search text I am getting index out of range issue
        let item = section.result[indexPath.row]

                let detailsVC = self.storyboard?.instantiateViewController(withIdentifier: "detailviewcontroller") as! DetailViewController
                let navigationController = UINavigationController(rootViewController: detailsVC)
                self.present(navigationController, animated: true, completion: nil)


            // Search Dismiss - without search text if click tableview cell I am getting crash 
            // MARK: Validate SearchController isActive or Not.
        if searchController != nil {
            // Get rid of searchController
            searchController.searchBar.endEditing(true)
            searchController.isActive = false
            searchController.dismiss(animated: true) { /* */ }
        }
        }

标签: iosswift

解决方案


这可能是因为您的 searchController 尚未启动。只是先检查一下searchController != nil


推荐阅读