我正在 tableView
中搜索,但是 NSDictionry
没有成员项,它没有过滤搜索
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {arrFilterLocation = searchText.isEmpty ? arrInrestLocation:arrInrestLocation.filter({ (item:String) -> Bool in
return item.range(of: searchText,options: .caseInsensitive ,range: nil,locale: nil) != nil
})
TabelView.reloadData()
}
最佳答案
假设arrFilterLocation
和arrInrestLocation
都是String
的数组
,即
var arrInrestLocation: [String]
var arrFilterLocation: [String]
您可以简单地在每个 string
上使用 contains(_:)
来 filter
出所有 strings
arrInrestLocation
包含 searchText
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
arrFilterLocation = searchText.isEmpty ? arrInrestLocation : arrInrestLocation.filter({ $0.lowercased().contains(searchText.lowercased()) })
tableView.reloadData()
}
关于ios - 我们可以将搜索应用于从 json 解析中获取的数组的 TableView 吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56579675/