Swift filterContentForSearchText

标签 swift uisearchbar

以下 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/

相关文章:

swift - Swift 编译器中缺少优化级别面板 - 代码生成

ios - 导航栏中的标题文本不会立即截断

swift - MacOS Playground : How can change the background color?

swift - 使用解析快速搜索栏

ios - 为什么字体是可选的?

iOS:如何在 Swift 中使用 Objective-C 中的#define?

iphone - iOS UISearchBar 在导航栏 iPhone 中的横向模式高度

ios - 如何使用多个条件过滤托管对象实体

swift 3 : UISearchBar in Popover ViewController shows up wider than expected (on iPad)

ios - SearchBar 加载 plist 时出现问题