ios - UISearchController UITableView didSelectRowAtIndexPath 在过滤后不起作用

标签 ios uitableview uisearchbar uisearchcontroller dismiss

我有一个 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 被关闭。

这有什么问题吗?

最佳答案

首先尝试在 searchController 上调用 dismiss

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

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

关于ios - UISearchController UITableView didSelectRowAtIndexPath 在过滤后不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53169628/

相关文章:

iphone - 申报属性(property)的最佳方式是什么?

ios - VStack 不适合 ContentView SwiftUI

ios - 当其中一项被删除时如何调整TableView高度

ios - 当用户触摸 UIBarButtonItem 时,在 tableView 上显示 UISearchBar

iphone - 增加 TableView 标题的大小

ios - UISearchBar 结束编辑,隐藏键盘,但屏幕仍然变暗

ios - 用另一个数组排序数组

iphone - 音量硬件按钮输入

ios - 使用自动布局动画化 UIView 动画化 UITableView 的复选标记

objective-c - 分组 UITableView 部分的背景