表格视图中的Swift全文搜索 [英] Swift Full text search in tableview

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

问题描述

我已在StackOverflow中签入找不到我的查询的任何有效解决方案.

I have checked in StackOverflow Not found any valid solution of my query.

struct MyModel{
let title: String
let subTitle: String
let image: UIImage
}

现在,我想搜索标题并将其输出,返回mymodel过滤器数组. 假设这是我的标题数组.

Now I want to search on title and output it return mymodel filter array. suppose this is my title array.

[
"Swift CollectionView Xcode 11",
"Swift UITableView Xcode 11",
"Swift UICollectionView Xcode 11",
"Objective C UITableView Xcode 11",
"Objective C CollectionView Xcode 11",
"Objective C UICollectionView Xcode 11",
]

如果我搜索"collectionView"

If I search "collectionView" then,

"Swift UICollectionView Xcode 11","Swift CollectionView Xcode 11"-正确

"Swift UICollectionView Xcode 11", "Swift CollectionView Xcode 11" - Right

如果我搜索"collectionview Objective C",那么我想要这个搜索结果,它给了我整个带有过滤器的数组

If I search "collectionview Objective C" then, I want this search result and it gives me my whole array with filter

"Objective C CollectionView Xcode 11", "Objective C UICollectionView Xcode 11", "Swift CollectionView Xcode 11", "Swift CollectionView Xcode 11",

"Objective C CollectionView Xcode 11", "Objective C UICollectionView Xcode 11", "Swift CollectionView Xcode 11", "Swift CollectionView Xcode 11",

因为在搜索中它也包含collectionview和目标c.首先,如果它包含整个字符串,那么结果必须先显示我,然后再显示其他.

because in search it contain collectionview and objective c too. first if it contain whole string that result has to show me first then other.

我尝试这种方式,但是没用

I try this way but it not work

let stringComponent = searchText.components(separatedBy: " ")
_ = searchText.components(separatedBy: " ").map{ (str) in
            arrTemp += arrVideo.filter { (data) -> Bool in
                if data.title.lowercased().contains(str.lowercased()) && !arrTemp.contains(data){
                    return true
                }
                return false
}

还检查了NSPredicate, Sort and filter,但对我不起作用.

Also checked NSPredicate, Sort and filter but not working for me.

请帮助我!谢谢.

推荐答案

检查以下代码,它将根据最佳搜索结果对数组进行排序.

Check the below code, it will sort the arrays as per best search result.

  let arr =  [
            "Swift CollectionView Xcode 11",
            "Swift UITableView Xcode 11",
            "Swift UICollectionView Xcode 11",
            "Objective C TableView Xcode 11",
            "Objective C CollectionView Xcode 11",
            "Objective C UICollectionView Xcode 11",
        ]

        //Sort the array and in dictioary according to search result
        let dups = Dictionary(grouping: self.serchedArray(arr: arr, isInsideCheck: true), by: {$0}).sorted { $0.1.count > $1.1.count }

        var resultArr2 : [String] = []
        for (_, value) in dups {
            resultArr2 = resultArr2 + self.serchedArray(arr: value, isInsideCheck: false)
        }

        let dups2 = Dictionary(grouping: resultArr2, by: {$0}).sorted { $0.1.count > $1.1.count }

        //Remove duplicate values from dictionary and map to array
        let finalArray = dups2.map { $0.value[0] }
        print(finalArray)

此功能是为搜索而创建的

This Function created for searching

   func serchedArray(arr : [String], isInsideCheck:Bool) -> [String] {
        var resultArr : [String] = []
        let searchText = "collectionview Objective C"
        let _ = searchText.components(separatedBy: " ").map { (str) in
            let searchedArr = arr.filter( {
                if $0.lowercased().components(separatedBy: " ").contains(str.lowercased()) {
                    return true
                } else {
                    if isInsideCheck {
                        //Check any word is contained in array's element
                        let words = $0.lowercased().components(separatedBy: " ")
                        for word in words {
                            if word.count > 1 {
                                if word.contains(str){
                                    return true
                                } else if str.contains(word) {
                                    return true
                                }
                            }
                        }
                     }
                    return false
                }
            })
            //Merge the array with previous result
            resultArr = resultArr + searchedArr
        }
        return resultArr
    }

它将返回Objective C CollectionView Xcode 11作为finalArray中的第一个元素,因为它与所有三个词(即collectionviewObjectiveC)匹配.

It will return Objective C CollectionView Xcode 11 as first element in finalArray because it matches the all three words i.e. collectionview, Objective, C.

代码很长,因为我们正在检查数组的每个元素中的searchString的每个单词, 首先我们逐字检查,然后进行排序.

The code is lengthy because we are checking each word of searchString in each element of array, first we check word by word then we sort.

希望这会为您提供帮助.. !!

Hope this will help you..!!

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

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