【问题标题】:UISearchController UITableView didSelectRowAtIndexPath not working after filterUISearchController UITableView didSelectRowAtIndexPath 过滤后不起作用
【发布时间】:2019-04-09 17:05:11
【问题描述】:

我有一个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
}

效果很好。每次用户更新UISearchBar 字段时,我都可以过滤UITableView

我的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 被解雇。

这有什么问题?

【问题讨论】:

标签: ios uitableview uisearchbar uisearchcontroller dismiss


【解决方案1】:

您需要在UISearchViewController 活动时将其关闭。

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  tableView.deselectRow(at: indexPath, animated: true)
  if searchController.isActive {
     searchController.dismiss(animated: true) {
      // Go to next screen
    }
  } else {
    // Go to next screen
  }
}

【讨论】:

    【解决方案2】:

    请确保您正在创建这样的搜索控制器。

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

    尤其是这一行。这解决了我在 ios 12 中的问题

    dimsBackgroundDuringPresentation = false
    

    一旦 didSelectRow 方法开始被调用,将这行代码关闭搜索控制器。

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

    【讨论】:

    • 只是重新审视这个,因为 dimsBackgroundDuringPresentation 在 iOS 12 之后已经过时:我们现在应该使用 obscuresBackgroundDuringPresentation
    【解决方案3】:

    先尝试在 searchController 上调用dismiss

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        searchController.dismiss(animated: false)
        dismiss(animated: true)
    }
    

    我刚刚对此进行了测试,无论搜索是否处于活动状态,它都有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-02
      • 2012-09-02
      • 1970-01-01
      相关资源
      最近更新 更多