以下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"]
中选择感兴趣的范围?
感谢任何帮助。
答案 0 :(得分:1)
查看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)
})
}