OGeek|极客世界-中国程序员成长平台

标题: ios - 单击 tableview 单元格时滚动到顶部搜索栏 Controller [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 14:39
标题: ios - 单击 tableview 单元格时滚动到顶部搜索栏 Controller

当我写文本时,searchController 移到顶部,但是当我单击 tableview 单元格时,我想要相同的行为。 searchController 需要将其设置为在 appstore 应用程序上的工作。如下所示,我点击 tableview 单元格,所以我希望 searchController 移到顶部。所以我必须在我的代码中写什么。谢谢

enter image description here

enter image description here



Best Answer-推荐答案


使 UISearchBar 中的 UITextField 成为点击单元格时的第一响应者()。

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {


        if let searchField = searchController.searchBar.value(forKey: "searchField") as? UITextField {
            searchField.becomeFirstResponder()
        }
    }

关于ios - 单击 tableview 单元格时滚动到顶部搜索栏 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49898358/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4