我使用下面的数组结构来创建一个包含部分的tableView
struct 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()) }
}不幸的是,因为科普特是一个字符串,而不仅仅是一个字符串,所以代码不能工作。是否有一种方法可以对此进行修改,以过滤对科普特分段的搜索?
发布于 2017-10-26 05:13:16
你可以这样做。
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",科普特语:“苹果”,“球”,“猫”,“狗”,英语:"")]
搜索“应用程序”
输出单词(sectionName: abc,科普特:"apple",英语:“”)
https://stackoverflow.com/questions/46945634
复制相似问题