Guest User

Untitled

a guest
Feb 18th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.50 KB | None | 0 0
  1. func setupUI() {
  2. self.view.backgroundColor = UIColor.whiteColor()
  3.  
  4. // Required to properly display searchbar within nav & tabbar controllers
  5. self.extendedLayoutIncludesOpaqueBars = true // have tried setting this to false as well
  6. self.definesPresentationContext = true
  7.  
  8. self.searchResultsController = AppDelegate.getViewController(ScheduleStoryboard.name, controllerName: ScheduleStoryboard.Identifiers.foodSearchResults) as? SearchResultsController
  9.  
  10. self.searchController = UISearchController(searchResultsController: searchResultsController)
  11. self.searchController.searchResultsUpdater = self
  12. self.searchController.delegate = self
  13. self.searchController.dimsBackgroundDuringPresentation = true
  14.  
  15. self.searchController.searchBar.delegate = self
  16. self.searchController.searchBar.placeholder = "Search foods..."
  17. self.searchController.searchBar.setBackgroundImage(UIImage(named: "background-searchbar")?.resizableImageWithCapInsets(UIEdgeInsetsMake(0, 0, 0, 0)), forBarPosition: .Any, barMetrics: .Default)
  18. self.searchController.searchBar.tintColor = UIColor.whiteColor()
  19. self.searchController.searchBar.sizeToFit()
  20.  
  21. // this headerView does NOT belong to the tableView, its anchored on top of the tableView so that the searchbar remains fixed when scrolling
  22. self.headerView.addSubview(searchController.searchBar)
  23.  
  24. self.tableView.delegate = self
  25. self.tableView.dataSource = self
  26. self.tableView.tableHeaderView?.backgroundColor = UIColor.clearColor()
  27. self.tableView.tableHeaderView?.addBorder(.Bottom, color: UIColor.groupTableViewBackgroundColor(), width: 0.25)
  28.  
  29. self.segmentedControl.tintColor = UIColor.genioBlue()
  30. }
  31.  
  32. class SearchController: UITableViewController {
  33. let searchController = UISearchController(searchResultsController: nil)
  34. var items:[ArrayOfYourType]
  35. var filteredItems:[ArrayOfYourType]
  36. var scopeTitles:[String]?
  37.  
  38. override func viewDidLoad() {
  39. super.viewDidLoad()
  40.  
  41. searchController.searchResultsUpdater = self
  42. searchController.dimsBackgroundDuringPresentation = false
  43. definesPresentationContext = true
  44. tableView.tableHeaderView = searchController.searchBar
  45.  
  46. searchController.searchBar.scopeButtonTitles = scopeTitles
  47. searchController.searchBar.delegate = self
  48. }
  49.  
  50. // MARK: - Table view data source
  51.  
  52. override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
  53. // #warning Incomplete implementation, return the number of sections
  54. return 1
  55. }
  56.  
  57. override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  58. if searchController.active {
  59. return filteredItems.count
  60. }
  61. return items.count
  62. }
  63.  
  64. func filterContentForSearchText(searchText: String, scope: String = "All") {
  65. filteredItems = items.filter { item in
  66. //return true or false depending on your filter
  67. return true
  68. }
  69. dispatch_async(dispatch_get_main_queue(), { () -> Void in
  70. self.tableView.reloadData()
  71. })
  72. }
  73.  
  74. override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  75. let cell = UITableViewCell(style: UITableViewCellStyle.Subtitle,
  76. reuseIdentifier: nil)
  77. let item: String
  78. let category: String
  79. if searchController.active {
  80. item = filteredItems[indexPath.row].getTitle()
  81. category = filteredItems[indexPath.row].getCategory()
  82. }
  83. else {
  84. item = items[indexPath.row].getTitle()
  85. category = items[indexPath.row].getCategory()
  86. }
  87. cell.textLabel?.text = item
  88. cell.detailTextLabel?.text = category
  89. return cell
  90. }
  91.  
  92. override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
  93. //your code here
  94. }
  95. }
  96.  
  97. //MARK: UISearchResultsUpdating
  98. extension SearchController: UISearchResultsUpdating {
  99. func updateSearchResultsForSearchController(searchController: UISearchController) {
  100. if let _ = scopeTitles {
  101. let searchBar = searchController.searchBar
  102. let scope = searchBar.scopeButtonTitles![searchBar.selectedScopeButtonIndex]
  103. filterContentForSearchText(searchController.searchBar.text!,scope:scope)
  104. }
  105. else {
  106. filterContentForSearchText(searchController.searchBar.text!)
  107. }
  108. }
  109. }
  110.  
  111. //MARK: UISearchBarDelegate
  112. extension SearchController: UISearchBarDelegate {
  113. func searchBar(searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
  114. filterContentForSearchText(searchBar.text!, scope: searchBar.scopeButtonTitles![selectedScope])
  115. }
  116. }
Add Comment
Please, Sign In to add comment