UISearchController中的UITableViewController
我已经建立了一个UISearchController作为标题为我的tableview用下面的代码:UISearchController中的UITableViewController
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功能,并在打印(searchUrl)断点的内函数,但它从来没有去过断点。为什么searchBarSearchButtonClicked函数在我点击搜索栏时没有执行?
您正在设置错误的代表。一个UISearchBarController
为代表的财产,但您所用的方法是UISearchBarDelegate
方法,所以你需要设置你的对象作为委托包含了`UISearchBarController内的搜索栏:
mySearchController.searchBar.delegate = self
这工作,谢谢! –
通常你会实现searchBar(textDidChange:),并在用户输入时执行搜索。使用计时器,以便仅在用户停止键入半秒时执行搜索 – Paulw11
如何在textDidChange函数内部实现计时器? –
你为什么铸造'自我作为UISearchControllerDelegate?这种低调可能返回'nil'。如果您在课堂中添加了实施的“UISearchControllerDelegate”,则不需要downcast。 – Paulw11
我删除了'mySearchController.delegate = self as? UISearchControllerDelegate'但它仍然不起作用 –
你需要有一个委托,你需要将你的类设置为委托,但我想知道为什么你有一个有条件的downcast来设置委托;它使我认为你没有正确设置委托实现,这就是为什么委托方法没有被调用。 – Paulw11