gpt4 book ai didi

ios - 使用多个过滤器搜索 TableView

转载 作者:行者123 更新时间:2023-11-30 14:11:11 25 4
gpt4 key购买 nike

我想问一下如何在uisearchDisplayController中使用多个过滤器进行搜索

这是我的方法:

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)

最佳答案

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

这是我在 Playground 上想出的一个简单示例。

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

关于ios - 使用多个过滤器搜索 TableView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31753736/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com