【问题标题】:How to use UISearchBarController with array subsection如何将 UISearchBarController 与数组小节一起使用
【发布时间】:2018-04-07 07:45:41
【问题描述】:

我正在使用下面的数组结构来创建一个带有节的 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()) }

    }

不幸的是,因为 coptic 是一个 [String],而不仅仅是一个 String,所以代码不起作用。有没有办法修改它以便能够过滤对科普特小节的搜索?

【问题讨论】:

    标签: arrays swift tableview uisearchbar sub-array


    【解决方案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)
            })
    
       }
    }
    

    输入数组 = 数组 = [words(sectionName: "abc", 科普特语: ["apple","ball","cat","dog"], 英语: [""])]

    搜索“应用”

    输出单词(sectionName:abc,科普特语:["apple"],英语:[""])]

    【讨论】:

    • 我之前问过同样的问题,没有人回答。我附上了我正在使用的完整代码的链接。如果您能给我任何帮助,我将不胜感激。谢谢你。 stackoverflow.com/questions/46854927/…
    • 我试着回答你的两个问题
    猜你喜欢
    • 2013-11-29
    • 1970-01-01
    • 2017-04-23
    • 2012-01-20
    • 2020-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-01
    相关资源
    最近更新 更多