简体   繁体   English

UISearchController不在UITableViewController中搜索

[英]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.

相关问题 使用UISearchController和Objective-C搜索UITableView而不是UITableViewController - Searching a UITableView not a UITableViewController with UISearchController and Objective-C UISearchController搜索两个数组 - UISearchController searching two arrays 使用UISearchController按对象属性搜索? - Searching by object property with UISearchController? 取消UISearchController会破坏分组的UITableViewController视觉效果 - Dismissing UISearchController breaks grouped UITableViewController visual UITableViewController(结果控制器)中带有UISearchController的自定义单元格 - Custom cell with UISearchController in UITableViewController (result controller) 需要像UISearchDisplayController一样使用没有UITableViewController的UISearchController - Need to use UISearchController without UITableViewController like a UISearchDisplayController 如何将segmentedControl和UISearchController作为UITableViewController标头? (编程) - How to have segmentedControl and UISearchController as UITableViewController header? (Programmatically) 用于UISearchController的自定义UITableViewController覆盖了iOS 11中的搜索栏 - Custom UITableViewController for UISearchController overlays the search bar in iOS 11 UITableViewController中的UISearchController-Rows不可选,表视图显示为灰色 - UISearchController in a UITableViewController—Rows not selectable and table view is greyed out 使用swift 3中的UISearchBar / UISearchController进行即时搜索 - Instant searching using the UISearchBar/UISearchController in swift 3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM