首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在数组分段中使用UISearchBarController

如何在数组分段中使用UISearchBarController
EN

Stack Overflow用户
提问于 2017-10-26 03:59:09
回答 1查看 86关注 0票数 0

我使用下面的数组结构来创建一个包含部分的tableView

代码语言:javascript
复制
struct words {

    var sectionName : String!
    var coptic : [String]!
    var english : [String]!
}

var array = [words]()
var filtered = [words]()

array = [words(sectionName: "", coptic: [""], English: [""])]

我想使用与此类似的代码使用搜索控制器

代码语言:javascript
复制
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()) }

    }

不幸的是,因为科普特是一个字符串,而不仅仅是一个字符串,所以代码不能工作。是否有一种方法可以对此进行修改,以过滤对科普特分段的搜索?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-10-26 05:13:16

你可以这样做。

代码语言:javascript
复制
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",英语:“”)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46945634

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档