Search table view using multiple filters

时间:2015-07-31 19:26:02

标签: ios swift

I would like to ask how can I search with multiple filters in searchDisplayController

here is my method:

func filterContentForSearchText(searchText: String, scope: String = "All") {
    // Filter the array using the filter method
    self.filteredCandies = self.person.filter({( candy: Candy) -> Bool in

        let categoryMatch = (scope == "All") || (candy.category == scope)

        let stringMatch = candy.name.rangeOfString(searchText)

        return categoryMatch && (stringMatch != nil)

1 个答案:

答案 0 :(得分:0)

您应该做的是在分隔符(空格)上拆分搜索文本,并根据您可能正在进行的不同搜索过滤结果。

以下是我在操场上提出的一个简单示例。

import Foundation
import UIKit

var searchText = "i F"
var arrayOfThingsIWantToSearch: [String] = ["One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"]
var searchTerms: [String] = split(searchText) {$0 == " "} //split out our search text on spaces to get multiple search terms

var filtered: [String] = []

for item in arrayOfThingsIWantToSearch {
    for searchTerm in searchTerms {
        if (item as NSString).containsString(searchTerm) {
            filtered.append(item)
            break
        }
    }
}

print(filtered) //prints "[Four, Five, Six, Eight, Nine]"