Swift Full text search in tableview

放肆的年华 提交于 2020-05-28 09:29:19

问题


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

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

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",
]

If I search "collectionView" then,

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

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",

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
}

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

Please help me! Thank You.


回答1:


from what I see the problem is when you separate the string from the empty string " ", so what happens is the keywords are formed

- collectionview
- Objective
- C

and cause all data to appear because "C" is a separate keyword. is it true that I say that all the data appears?




回答2:


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
    }

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.

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..!!



来源:https://stackoverflow.com/questions/61932628/swift-full-text-search-in-tableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!