gpt4 book ai didi

ios - 如何像 iOS "Contacts"应用程序一样显示搜索结果

转载 作者:行者123 更新时间:2023-11-28 07:45:51 26 4
gpt4 key购买 nike

我是 iOS 编程新手。我试图将我的应用程序实现为看起来像 iOS 联系人应用程序。但我不知道如何实现我想要的。我想让搜索结果看起来像默认的 iOS 应用。

看看我到目前为止尝试了什么:

When i type something the dimsBackgroundDuringPresentation still <code>true</code>

当我输入内容时,dimsBackgroundDuringPresentation 仍然是 true

这是我的期望:

Here is detail of my expectation

我想知道这个应用程序如何显示这样的结果。

这是我声明 UISearchController 的方式

lazy var searchController: UISearchController = ({

let controller = UISearchController(searchResultsController: nil)

controller.hidesNavigationBarDuringPresentation = false
controller.searchBar.sizeToFit()
controller.searchBar.backgroundColor = UIColor.clear
controller.searchBar.placeholder = "Search"
controller.dimsBackgroundDuringPresentation = true

return controller

})()

下面是我如何将 searchBar 初始化为 tableView 的标题部分

let searchBar = searchController.searchBar
tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
searchBar.delegate = self

tableView.tableHeaderView = searchController.searchBar

这是委托(delegate) UISearchResultsUpdating

的函数
func updateSearchResults(for searchController: UISearchController) {
if let count = searchController.searchBar.text?.count {
if count > 0 {

filterArray.removeAll(keepingCapacity: false)
var a = [String]()
a = [searchController.searchBar.text!]
filterArray = a
searchController.dimsBackgroundDuringPresentation = false
tableView.reloadData()
}else {
searchController.dimsBackgroundDuringPresentation = true
filterArray.removeAll(keepingCapacity: false)
filterArray = array
tableView.reloadData()
}
}

}

这是我的 tableView 单元格的样子

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if searchController.isActive {
return filterArray.count
}else {
return array.count
}

}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)

if searchController.isActive {
cell.textLabel?.text = filterArray[indexPath.row]
}else {
cell.textLabel?.text = array[indexPath.row]
}


return cell

}

最佳答案

试试这些选项:

let search = UISearchController(searchResultsController: nil)
self.definesPresentationContext = true
search.dimsBackgroundDuringPresentation = false
navigationItem.searchController = search
navigationItem.hidesSearchBarWhenScrolling = false

试试我的 repo 中的代码,看看它是否工作,只需点击一个 tableview 项目,它就会转到第二个 Controller ,在那里你可以看到搜索栏,然后点击它看看这是否是你想要的

github link

关于ios - 如何像 iOS "Contacts"应用程序一样显示搜索结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51088903/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com