[英]UISearchController not searching in UITableViewController
I have set up a UISearchController as a header for my tableview with the following code: 我已经使用以下代码将UISearchController设置为表视图的标题:
let mySearchController = UISearchController(searchResultsController: nil)
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
let keywords = searchBar.text
let finalKeywords = keywords?.replacingOccurrences(of: " ", with: "+")
searchUrl = "https://api.spotify.com/v1/search?q=\(finalKeywords!)&type=track&limit=20"
print(searchUrl)
callAlamo(url: searchUrl)
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(postCell.self, forCellReuseIdentifier: cellId)
tableView.separatorColor = .red
mySearchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
mySearchController.delegate = self as? UISearchControllerDelegate
tableView.tableHeaderView = mySearchController.searchBar
}
I adde a breakpoint at the searchBarButtonClicked function and at the print(searchUrl) inside of the function, but it never went to the breakpoints. 我在searchBarButtonClicked函数和函数内部的print(searchUrl)处添加了一个断点,但是它从未到达断点。 Why is the searchBarSearchButtonClicked function not executing when I click on the search bar? 当我单击搜索栏时,为什么searchBarSearchButtonClicked函数没有执行?
You are setting the wrong delegate. 您设置了错误的委托。 A UISearchBarController
as a delegate property, but the method you are implementing is a UISearchBarDelegate
method, so you need to set your object as the delegate for the search bar contained within the `UISearchBarController: UISearchBarController
作为委托属性,但是您要实现的方法是UISearchBarDelegate
方法,因此您需要将对象设置为UISearchBarController中包含的搜索栏的委托:
mySearchController.searchBar.delegate = self
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.