繁体   English   中英

UISearchController不在UITableViewController中搜索

[英]UISearchController not searching in UITableViewController

我已经使用以下代码将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

}

我在searchBarButtonClicked函数和函数内部的print(searchUrl)处添加了一个断点,但是它从未到达断点。 当我单击搜索栏时,为什么searchBarSearchButtonClicked函数没有执行?

您设置了错误的委托。 UISearchBarController作为委托属性,但是您要实现的方法是UISearchBarDelegate方法,因此您需要将对象设置为UISearchBarController中包含的搜索栏的委托:

mySearchController.searchBar.delegate = self

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM