我有一个像这样的数组模型:
class Assess_order_med_model
{
var name:String!
var id:String!
init(name:String!,id:String!) {
self.name = name
self.id = id
}
}
我希望根据" id"过滤我的数组。为此,我这样做。
var filteredData = [Assess_order_med_model]()
override func viewDidLoad() {
super.viewDidLoad()
table_View.dataSource = self
table_View.delegate = self
searchBar.delegate = self
searchBar.showsCancelButton = false
self.table_View.allowsSelection = false
let array_vale1 = Assess_order_med_model.init(name: "New York, NY", id: "1")
self.array_model.append(array_vale1)
filteredData = array_model
}
现在我正在过滤我的数组
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
filteredData = searchText.isEmpty ? array_model : array_model.filter({(dataString: String) -> Bool in
// If dataItem matches the searchText, return true to include it
return dataString.range(of: searchText, options: .caseInsensitive) != nil
})
table_View.reloadData()
}
但它给出了我这样的错误:Assess_order_model类型的值没有成员范围。 在进行过滤时,任何人都可以帮我解决我做错的事吗?
答案 0 :(得分:1)
您的array_model
包含对象Assess_order_med_model
,请更改您的textDidChange searchText
,如下所示
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
let searchString = searchText.trimWhiteSpace()
if searchString != "", searchString.count > 0 {
let filterData = array_model.filter {
return $0.id?.range(of: searchString, options: .caseInsensitive) != nil
}
}
}
这是trimWhiteSpace
,
extension String {
func trimWhiteSpace() -> String {
let string = self.trimmingCharacters(in: .whitespacesAndNewlines)
return string
}
}