IOS Swift 从数组中搜索表 [英] IOS Swift Searching table from an Array

查看:15
本文介绍了IOS Swift 从数组中搜索表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始学习 swift,我正在研究 tableview 和 searchbar 功能.下面我有我的数组,它是水果列表:

var fruits: [[String]] = [["Apple", "Green"],["Pear", "Green"], ["Banana", "Yellow"], ["Orange", 橘子"]]

我将它们放在表格视图中,以水果的名称作为标题,以颜色作为副标题.我正在尝试使用搜索栏进行过滤,但我似乎无法正确使用.我只想搜索水果的名称而不是颜色.

varfilteredFruits = [String]()var shouldShowSearchResults = falsefunc searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {过滤的Fruits.removeAll()变量 i = 0当我 <水果数{varfilteredFruits =fruits[i].filter({ (fruit: String) -> Bool in返回fruit.lowercased().range(of: searchText.lowercased()) != nil})如果搜索文本 != ""{shouldShowSearchResults = true如果filteredItems.count >0{filteredFruits.append(filteredItems[0])过滤项目.removeAll()}}别的{shouldShowSearchResults = false}我 += 1}self.tableView.reloadData()}

我确实得到了返回的结果,但它混淆了字幕和标题,并且没有返回正确的结果.有人能指出我正确的方向吗?

解决方案

我不明白你为什么使用某种 while 循环迭代结果.相反,我建议您利用以下功能:

func filterFruits(searchText: String) ->[[细绳]] {守卫搜索文本!="其他{回果}让针 = searchText.lowercased()返回fruits.filter {fruitObj in返回fruitObj.first!.lowercased().contains(needle)}}

该函数返回名称中包含 searchText 的所有水果.

<块引用>

filterFruits(searchText: "g") 产生 [["Orange", "Orange"]]

如果要搜索所有属性,请使用以下内容:

func filterFruits(searchText: String) ->[[细绳]] {守卫搜索文本!="其他{回果}让针 = searchText.lowercased()返回fruits.filter {fruitObj in返回fruitObj.contains { 属性在attribute.lowercased().contains(needle)}}}

<块引用>

filterFruits(searchText: "g") 产生 [["Apple", "Green"], ["Pear", "Green"], ["Orange", "Orange""]]

为了让您在未来走上正轨:您应该真正引入一个 Fruit 类,该类包含一个特定水果实例的所有相关信息.然后您可以使用第一个函数并执行类似 fruitObj.matches(searchText) 的操作,其中您在 Fruit 类中定义了一个 func 来确定是否水果与搜索匹配.

I have just started to learn swift and i am looking at the tableview and searchbar feature. Below i have my array which is a list of fruits:

var fruits: [[String]] = [["Apple", "Green"],["Pear", "Green"], ["Banana", "Yellow"], ["Orange", "Orange"]]

I have them in a table view with the name of the fruit as the title and the colour as a subtitle. I am trying to use the search bar to filter but i cant seem to get it right. I only want to search for the name of the fruit not the colour.

var filteredFruits = [String]()
var shouldShowSearchResults = false

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {

    filteredFruits.removeAll()

    var i = 0
    while i < fruits.count
        {
            var filteredFruits = fruits[i].filter ({ (fruit: String) -> Bool in
                return fruit.lowercased().range(of: searchText.lowercased()) != nil
            })
            if searchText != ""
            {
                shouldShowSearchResults = true

                if filteredItems.count > 0
                {
                    filteredFruits.append(filteredItems[0])
                    filteredItems.removeAll()
                }
            }
            else
            {
                shouldShowSearchResults = false

            }

            i += 1
        }

    self.tableView.reloadData()
}

I do get results returned but it mixes up the subtitles and the titles as well as not returning the correct results. Can anyone point me in the right direction?

解决方案

I do not understand why you iterate over the fruits using some kind of while loop. Instead I would propose you take advantage of a function like:

func filterFruits(searchText: String) -> [[String]] {
    guard searchText != "" else {
        return fruits
    }
    let needle = searchText.lowercased()
    return fruits.filter {fruitObj in
        return fruitObj.first!.lowercased().contains(needle)
    }
}

That function returns all fruits that have a name containing the searchText.

filterFruits(searchText: "g") yields [["Orange", "Orange"]]

If you want to search through all attributes use something like:

func filterFruits(searchText: String) -> [[String]] {
    guard searchText != "" else {
        return fruits
    }
    let needle = searchText.lowercased()
    return fruits.filter {fruitObj in
        return fruitObj.contains { attribute in
            attribute.lowercased().contains(needle)
        }
    }
}

filterFruits(searchText: "g") yields [["Apple", "Green"], ["Pear", "Green"], ["Orange", "Orange"]]

To get you on the right track for the future: you should really introduce a Fruit class which holds all relevant information of one specific fruit instance. Then you can use the first function and do something like fruitObj.matches(searchText) where you define a func inside the Fruit class which determines if the fruit matches the search.

这篇关于IOS Swift 从数组中搜索表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆