@IBOutlet weak var tableViewhospitalss: UITableView!
@IBOutlet weak var searchBar: UISearchBar!
//list to store all the Hospitals
var hospitalsList = [hospitalsModel]()
var searchActive : Bool = false
var filtered:[String] = []
以及funcs
func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
searchActive = true
}
func searchBarTextDidEndEditing(searchBar: UISearchBar) {
searchActive = false
}
func searchBarCancelButtonClicked(searchBar: UISearchBar) {
searchActive = false
}
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
searchActive = false
}
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
filtered = hospitalsList.filter ({ (text) -> Bool in
let temp: NSString = text! as NSString
let range = temp.range(of: searchText, options: NSString.CompareOptions.caseInsensitive)
return range.location != NSNotFound
})
if(filtered.count == 0){
searchActive = false;
} else {
searchActive = true;
}
self.tableViewhospitalss.reloadData()
}
enter image description here
任何人有任何建议请给我错误
无法将类型“[hospitalsModel]”的值赋给类型“[String]”
最佳答案
问题是您正试图筛选并将“hospitalsModel”转换为字符串,因为您筛选的变量是[String],请尝试使用map或filter->返回相同的类型。
关于swift - 具有Firebase数据库和Swift的UISearchBar函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49542976/