@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()
}
任何人都有任何建议请它给我错误 无法指定类型'[hospitalsModel]'的值来键入'[String]'
答案 0 :(得分:0)
问题在于您尝试过滤并将'hospitalModel'转换为String,因为您的var过滤为[String],尝试使用map或filter - >返回相同的类型。
答案 1 :(得分:0)
class hospitalsModel:NSObject {
var id: String?
var name: String?
var subName: String?
var number: String?
var location: String?
var content: String?
var img: String?
var ta5sos: String?
var workTime: String?
var location2 : String?
var aamOrTa5sosy : String?
var latitude: Float?
var longtude: Float?
init(id: String?, name: String?, subName: String?, number :String?, location:String?,content: String?, img: String?, ta5sos: String?, workTime: String?, location2 : String?, aamOrTa5sosy : String?, latitude: Float? , longtude: Float?){
self.id = id
self.name = name
self.subName = subName
self.number = number
self.location = location
self.content = content
self.img = img
self.ta5sos = ta5sos
self.workTime = workTime
self.location2 = location2
self.aamOrTa5sosy = aamOrTa5sosy
self.latitude = latitude
self.longtude = longtude
}
} `
答案 2 :(得分:0)
class HospitalModel: NSObject {
var id: String?
var name: String?
var subName: String?
var number: String?
var location: String?
var content: String?
var img: String?
var ta5sos: String?
var workTime: String?
var location2 : String?
var aamOrTa5sosy : String?
var latitude: Float?
var longtude: Float?
init(id: String?, name: String?, subName: String?, number :String?, location:String?,content: String?, img: String?, ta5sos: String?, workTime: String?, location2 : String?, aamOrTa5sosy : String?, latitude: Float? , longtude: Float?){
self.id = id
self.name = name
self.subName = subName
self.number = number
self.location = location
self.content = content
self.img = img
self.ta5sos = ta5sos
self.workTime = workTime
self.location2 = location2
self.aamOrTa5sosy = aamOrTa5sosy
self.latitude = latitude
self.longtude = longtude
}
class func filterByName(collection: [HospitalModel], text: String) -> [HospitalModel] {
return collection.filter({ text.lowercased().range(of: $0.name ?? "") != nil })
}
class func filterBySubname(collection: [HospitalModel], text: String) -> [HospitalModel] {
return collection.filter({ text.lowercased().range(of: $0.subName ?? "") != nil })
}
}