ios - 如何在使用 Swift 单击 UITableview 单元格之前检查 SearchController 是否处于活动状态
问题描述
我正在SearchController
用tableview
数据实现。在这里,在单击搜索结果单元格以移动另一个详细信息页面之后搜索特定数据。现在,如果我回到主视图控制器搜索控制器显示而不解雇。我尝试了下面的代码它在主视图控制器场景的细节上工作正常,但是如果我单击没有搜索文本的单元格,那么它会崩溃。如何解决?
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
。
推荐阅读
- php - 用于签署推送通知令牌的 Apple 签名密钥的 x、y 和曲线参数是什么?
- java - 在 RecyclerView 中显示 SQLite DB 数组列表不断崩溃
- typescript - 使用 TypeScript 的捆绑器
- ios - 通过蜂窝连接连接时出现 SSL 错误
- python - 如何在 python 中读取文件并将其内容保存到二维数组?
- c++ - 条件运算符从其参数的副本返回值
- sql-server - 如何使用 SSIS 每周导入文件?
- c++ - 是否可以将引号 („) 放入 Char
- c - 解决结构数组及其中的 realloc 的一些建议
- c++ - Including a cpp file in a header file without multiple definition