gpt4 book ai didi

ios - 如何使用搜索在Swift中搜索多个字段?

转载 作者:行者123 更新时间:2023-12-01 16:21:45 25 4
gpt4 key购买 nike

我正在尝试获取搜索结果以从以下位置检索数据:
我的ProductList类的名称,药房名称,品牌,类别和名称,该类从Firestore检索数据作为搜索结果

我想我在哪里找到了解决方案,以使搜索正确进行,以便可以使用搜索中的所有必填字段

问题出在函数内的SearchbarDelegate扩展中,我只是不知道如何对其进行修改以使其名称,dispensaryName,品牌,类别和菌株全部一致地搜索并显示在搜索结果中

因为每次我运行它时,它都无法正常运行,它会在运行模拟器时查找一个字段或另一个字段,而不是同时查找所有五个字段

我已经测试了搜索栏的代码,但它似乎仍然无法正常工作

变种

   @IBOutlet weak var searchBar: UISearchBar!
@IBOutlet weak var productListTableView: UITableView!

var productInventory: [ProductList] = []
var productSetup: [ProductList] = []

TAVBLEVIEW
extension ProductListController: UITableViewDelegate, UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

if searchBar.text != "" {
return self.productInventory.count
}
return productSetup.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "ProductListCell") as?
ProductListCell else { return UITableViewCell() }

//search not empty
if searchBar.text != "" {
cell.configure(withProduct: productInventory[indexPath.row])
}else{

cell.configure(withProduct: productSetup[indexPath.row])
}



return cell
}

}

SEARCHBAR代理
    extension ProductListController : UISearchBarDelegate
{
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
productInventory = self.productSetup.filter({ (products) -> Bool in
return products.name.lowercased().range(of: searchText.lowercased()) != nil
})
productInventory = self.productSetup.filter({ (products) -> Bool in
return products.dispensaryName.lowercased().range(of: searchText.lowercased()) != nil
})
productInventory = self.productSetup.filter({ (products) -> Bool in
return products.brand.lowercased().range(of: searchText.lowercased()) != nil
})
productInventory = self.productSetup.filter({ (products) -> Bool in
return products.category.lowercased().range(of: searchText.lowercased()) != nil
})
productInventory = self.productSetup.filter({ (products) -> Bool in
return products.strain.lowercased().range(of: searchText.lowercased()) != nil
})

self.productListTableView.reloadData()
}
}

最佳答案

您只需要一个过滤器。只需将所有条件放入一个过滤器中即可。

productInventory = self.productSetup.filter({ (products) -> Bool in
return products.name.lowercased().range(of: searchText.lowercased()) != nil ||
products.dispensaryName.lowercased().range(of: searchText.lowercased()) != nil ||
products.brand.lowercased().range(of: searchText.lowercased()) != nil
// Add the rest as needed.
})

注意,使用带有适当选项的 range更好。
productInventory = self.productSetup.filter({ (products) -> Bool in
return products.name.range(of: searchText, options: [ .caseInsensitive, .diacriticInsensitive ]) != nil ||
products.dispensaryName.range(of: searchText, options: [ .caseInsensitive, .diacriticInsensitive ]) != nil ||
products.brand.range(of: searchText, options: [ .caseInsensitive, .diacriticInsensitive ]) != nil
// Add the rest as needed.
})

关于ios - 如何使用搜索在Swift中搜索多个字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58141400/

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