我正在尝试通过UISearchController
实现搜索栏并将代理实现为
func updateSearchResultsForSearchController(searchController: UISearchController) {
filteredTableData.removeAll(keepCapacity: true)
let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text)
let array = (tabledata as NSArray).filteredArrayUsingPredicate(searchPredicate)
filteredTableData = array as! [String]
self.tblView.reloadData()
}
但是,当我们点按搜索栏时,tableview
无法填充数据。否则一切正常。
我还尝试检查搜索栏的文本> 1,但它在初始时起作用但在退格时给我错误。什么是黑客?
答案 0 :(得分:1)
您应该实施UISearchbarDelegate's此方法
所以你的行将是
func searchBar(searchBar: UISearchBar, textDidChange searchText: String)
{
filteredTableData.removeAll(keepCapacity: true)
let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchBar.text)
let array = (tabledata as NSArray).filteredArrayUsingPredicate(searchPredicate)
filteredTableData = array as! [String]
self.tblView.reloadData()
}
您需要在此
之前将searchBar.delegate设置为self答案 1 :(得分:0)
我的问题的解决方案是
func updateSearchResultsForSearchController(searchController:UISearchController){
if count(searchController.searchBar.text) > 0{
let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text)
let array = (tabledata as NSArray).filteredArrayUsingPredicate(searchPredicate)
filteredTableData = array as! [String]
self.tblView.reloadData()
}else if count(searchController.searchBar.text) == 0 {
filteredTableData = tabledata
self.tblView.reloadData()
}
你可以像萨阿德那样做:
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
if count(searchBar.text) > 0{
let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchBar.text)
let array = (tabledata as NSArray).filteredArrayUsingPredicate(searchPredicate)
filteredTableData = array as! [String]
self.tblView.reloadData()
}else if count(searchBar.text) == 0 {
filteredTableData = tabledata
self.tblView.reloadData()
}
}