ios - 如何在使用 Swift 单击 UITableview 单元格之前检查 SearchController 是否处于事件状态

标签 ios swift

我正在使用 tableview 数据实现 SearchController。在这里,Once searched particular data after that clicking search result cell to moving another detail page.现在,如果我回到主视图 Controller 搜索 Controller 显示而不解雇。我尝试了下面的代码,它在主视图 Controller 场景的细节方面工作正常,但如果我单击没有搜索文本的单元格,那么它会崩溃。如何解决?

    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) { /* */ }
        }
        }

最佳答案

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

关于ios - 如何在使用 Swift 单击 UITableview 单元格之前检查 SearchController 是否处于事件状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57802372/

相关文章:

ios - 表格 View 在向下滚动的同时播放其他单元格中的视频

ios - 我在 ImageView 子类中定义手势识别器,但在 ViewController 中调用方法时崩溃并显示 "unrecognized selector"

swift - 类型 'StorageMetadata' 的值没有成员 'downloadURL'

swift - 多次按下开始倒计时的按钮会加快倒计时

ios - 使用ios swift取消订阅附近的api后如何禁用蓝牙?

ios - 带有标签和固定元素的横向模式下的 iPhone 6 Plus 奇怪错误

iOS - 在用户请求之前以适当的货币显示产品价格?

objective-c - 如何隐藏表索引和表?

ios - 实现不同的行为以在点击取消事件 View 时关闭 UIAlertController 操作表

swift - 如何知道 AVSpeechSynthesizer 何时说完所有排队的话语。 swift 4.2