gpt4 book ai didi

Swift filterContentForSearchText

转载 作者:可可西里 更新时间:2023-11-01 02:27:28 24 4
gpt4 key购买 nike

以下 Swift searchController 代码工作正常。

func filterContentForSearchText(searchText: String) {
searchResults = drugs.filter({ ( drugs: Drug) -> Bool in
let nameMatch = drugs.name.rangeOfString(searchText, options:
NSStringCompareOptions.CaseInsensitiveSearch)
return nameMatch != nil
})
}

上面的 Swift searchController.textSearch 适用于 Drug.name。数组的结构是:

struct Drug {
let name:String
let tumor:String
let targets:String
let dosing:String
let note:String
let chart:String
}

数组的一些行定义为:

Drug(name:"ABT-199 (Tivantinib)", tumor:"CLL Lymphoma", targets:"BCL-2, BCL2", dosing:"", note:"", chart:""), Drug(name:"Adcetris (Brentuximab vedotin)", tumor:"Anaplastic Large Cell Lymphoma Hodgkin's ALCL", targets:"[CD30]", dosing:"", note:"", chart:""), Drug(name:"Afinitor (Everolimus)", tumor:"Kidney Renal Hypernephroma Breast Brain Subependymal Giant Cell Astrocytoma (SEGA) tuberous sclerosis neuroendocrine pancreas (PNET)", targets:"[mTOR1][FRAP1]", dosing:"", note:"", chart:"")

我添加了一个只有三个按钮的范围栏。我对如何在 Swift 中更改上述内容感到困惑:

func filterContentForSearchText(searchText: String) 这样我就可以让用户从 scopeButtonTitles = ["Drugs", "Tumor", "Target"] ?

感谢任何帮助。

最佳答案

查看 Raywenderlich 的教程,其中解释了如何使用范围。我正在复制可能对您有帮助的代码片段。

http://www.raywenderlich.com/76519/add-table-view-search-swift

func filterContentForSearchText(searchText: String) {
// Filter the array using the filter method
self.filteredCandies = self.candies.filter({( candy: Candy) -> Bool in
let categoryMatch = (scope == "All") || (candy.category == scope)
let stringMatch = candy.name.rangeOfString(searchText)
return categoryMatch && (stringMatch != nil)
})
}

关于Swift filterContentForSearchText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26663329/

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