我使用以下数组结构来创建一个带有节
的tableViewstruct words {
var sectionName : String!
var coptic : [String]!
var english : [String]!
}
var array = [words]()
var filtered = [words]()
array = [words(sectionName: "", coptic: [""], English: [""])]
我想使用类似代码的搜索控件
func updateSearchResults(for searchController: UISearchController) {
// If we haven't typed anything into the search bar then do not filter the results
if searchController.searchBar.text! == "" {
filtered = array
} else {
// Filter the results
filtered = array.filter { $0.coptic.lowercased().contains(searchController.searchBar.text!.lowercased()) }
}
不幸的是,因为coptic是一个[String],而不仅仅是一个String,所以代码不起作用。有没有办法修改它以便能够过滤搜索coptic子部分?
答案 0 :(得分:1)
你可以这样做。
func updateSearchResults(for searchController: UISearchController) {
// If we haven't typed anything into the search bar then do not filter the results
if searchController.searchBar.text! == ""
{
filtered = array
}
else
{
filtered.removeAll()
array.forEach({ (word:words) in
var tempWord:words = words.init(sectionName: word.sectionName, coptic: [""], english: [""])
let copticArray = word.coptic.filter({ (subItem:String) -> Bool in
let a = subItem.lowercased().contains(searchController.searchBar.text!.lowercased())
return a;
})
tempWord.coptic = copticArray
filtered.append(tempWord)
})
}
}
输入数组=数组= [字数(sectionName:" abc",coptic:[" apple"," ball"," cat" ;," dog"],英语:[""])]
搜索" app"
输出字(sectionName:abc,coptic:[" apple"],英文:[""])]