UISearchController不在UITableViewController中搜索

时间:2017-05-18 22:50:31

标签: ios swift xcode uitableview

我已经使用以下代码将UISearchController设置为我的tableview的标题:

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函数没有执行?

1 个答案:

答案 0 :(得分:0)

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

mySearchController.searchBar.delegate = self