Guest User

Untitled

a guest
Mar 26th, 2018
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.79 KB | None | 0 0
  1. class CollectionViewController: UIViewController ,UICollectionViewDelegate,UICollectionViewDataSource,UISearchControllerDelegate, UISearchBarDelegate, UISearchResultsUpdating{
  2.  
  3. var items = [Food]()
  4. var filtered = [Food]()
  5. var searchActive : Bool = false
  6. let searchController = UISearchController(searchResultsController: nil)
  7.  
  8. // var items = ["ASIAN","SEAFOOD","SUSHI","CAFE","Los Angeles","Austin","Seattle"]
  9.  
  10.  
  11. @IBOutlet var collectionViewController: UICollectionView!
  12. override func viewDidLoad() {
  13. super.viewDidLoad()
  14. // imageViewArray=[#imageLiteral(resourceName: "food18") ,#imageLiteral(resourceName: "food19"),#imageLiteral(resourceName: "food20"),#imageLiteral(resourceName: "food21"),#imageLiteral(resourceName: "food22"),#imageLiteral(resourceName: "food23"),#imageLiteral(resourceName: "food24")]
  15.  
  16. self.items.append(Food(title: "View Appointment", image: UIImage(named: "view appointments@1x.png")))
  17. self.items.append(Food(title: "Medical Chart", image:UIImage(named: "medical chart@1x.png")))
  18. self.items.append(Food(title: "Task", image:UIImage(named: "tasks@2x.png")))
  19. self.items.append(Food(title: "Referrals", image:UIImage(named: "referrals@2x.png")))
  20. self.items.append(Food(title: "Profile", image:UIImage(named: "profile@2x.png")))
  21. self.items.append(Food(title: "Wallet", image:UIImage(named: "wallet@2x.png")))
  22.  
  23. collectionViewController.dataSource=self
  24. collectionViewController.delegate=self
  25.  
  26. self.searchController.searchResultsUpdater = self
  27. self.searchController.delegate = self
  28. self.searchController.searchBar.delegate = self
  29.  
  30. self.searchController.hidesNavigationBarDuringPresentation = false
  31. self.searchController.dimsBackgroundDuringPresentation = true
  32. self.searchController.obscuresBackgroundDuringPresentation = false
  33. searchController.searchBar.placeholder = "Search for tools and resources"
  34. searchController.searchBar.sizeToFit()
  35.  
  36. searchController.searchBar.becomeFirstResponder()
  37.  
  38. self.navigationItem.titleView = searchController.searchBar
  39.  
  40.  
  41.  
  42. // Do any additional setup after loading the view, typically from a nib.
  43. }
  44. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  45.  
  46. if searchActive {
  47. return filtered.count
  48. }
  49. else
  50. {
  51. return items.count //return number of rows in section
  52. }
  53. }
  54.  
  55. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  56.  
  57. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CategoriesCollectionViewCell
  58.  
  59. //configureCell(cell: cell as! toolCollectionViewCell, forItemAtIndexPath: indexPath as NSIndexPath)
  60. cell.imageViewCell=cell.viewWithTag(1) as! UIImageView
  61. if searchActive {
  62. cell.imageViewCell.image = self.filtered[indexPath.row].image
  63. cell.categoriesTitleLbl.text = self.filtered[indexPath.row].title
  64. } else {
  65. cell.imageViewCell.image = self.items[indexPath.row].image
  66. cell.categoriesTitleLbl.text = self.items[indexPath.row].title
  67. }
  68. return cell //return your cell
  69. }
  70.  
  71. func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
  72. searchActive = false
  73. self.dismiss(animated: true, completion: nil)
  74. }
  75.  
  76. func updateSearchResults(for searchController: UISearchController)
  77. {
  78. guard let searchString = searchController.searchBar.text else {
  79. return
  80. }
  81.  
  82. filtered = items.filter({ (item) -> Bool in
  83. if let title = item.title {
  84. return title.lowercased().contains(searchString.lowercased())
  85. } else {
  86. return false
  87. }
  88. })
  89.  
  90. collectionViewController.reloadData()
  91. }
  92.  
  93. func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
  94. searchActive = true
  95. collectionViewController.reloadData()
  96. }
  97.  
  98.  
  99. func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
  100. searchActive = false
  101. collectionViewController.reloadData()
  102. }
  103.  
  104. func searchBarBookmarkButtonClicked(_ searchBar: UISearchBar) {
  105. if !searchActive {
  106. searchActive = true
  107. collectionViewController.reloadData()
  108. }
  109.  
  110. searchController.searchBar.resignFirstResponder()
  111. }
  112.  
  113.  
  114.  
  115.  
  116.  
  117. }
  118. class Food {
  119. var title: String?
  120. var image: UIImage?
  121.  
  122. init(title: String?, image: UIImage?) {
  123. self.title = title
  124. self.image = image
  125. }
  126. }
  127.  
  128. class CategoriesCollectionViewCell: UICollectionViewCell {
  129.  
  130. @IBOutlet weak var imageViewCell: UIImageView!
  131. @IBOutlet weak var categoriesTitleLbl: UILabel!
  132. }
Add Comment
Please, Sign In to add comment