首页 > 解决方案 > UISearchController UITableView didSelectRowAtIndexPath 过滤后不起作用

问题描述

我有一个UIViewController通过展示它来打开的。在其中 avec 只有一个UITableViewUISearchController那个:

let searchController = UISearchController(searchResultsController: nil)

if #available(iOS 11.0, *) {
    searchController.searchResultsUpdater = self
    searchController.dimsBackgroundDuringPresentation = false
    navigationItem.searchController = searchController
    navigationItem.hidesSearchBarWhenScrolling = false
    definesPresentationContext = true
}

它运作良好。UITableView每次用户更新UISearchBar字段时,我都可以过滤。

我的func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)功能是这样的:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    dismiss(animated: true, completion: nil)
}

UItableView在过滤(当 UISearchBar 聚焦时)我第一次选择一行时,我func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)被调用但dismiss(animated: true, completion: nil)不起作用。它只是resignFristResponder().UISearchBar

然后,如果再次选择一个单元格,那么现在UISearchBar就不再关注该dismiss(animated: true, completion: nil)作品了。

所以我必须选择两次单元格才能看到我UIViewController被解雇。

那有什么问题?

标签: iosuitableviewuisearchbaruisearchcontrollerdismiss

解决方案


Just Makey sure you are creating your search controller like this.

let searchController: UISearchController = { let search = UISearchController(searchResultsController: nil) search.definesPresentationContext = true search.dimsBackgroundDuringPresentation = false return search }()

Especially this line. That solved my problem in ios 12

dimsBackgroundDuringPresentation = false

Once didSelectRow method starts getting called, put this line of code to dismiss Search Controller.

searchController.dismiss(animated: true, completion: nil)

推荐阅读