Advertisement
Guest User

Untitled

a guest
Jul 18th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1. //: Playground - noun: a place where people can play
  2.  
  3. import UIKit
  4.  
  5. var str = "Hello, playground"
  6.  
  7. let searchController = UISearchController(searchResultsController: nil)
  8.  
  9.  
  10. //in viewdidload
  11. searchController.searchBar.tintColor = .white
  12. searchController.searchBar.barTintColor = .white
  13. searchController.searchResultsUpdater = self
  14. searchController.searchBar.keyboardAppearance = .dark
  15. searchController.definesPresentationContext = false
  16. searchController.dimsBackgroundDuringPresentation = false
  17. UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self] ).tintColor = UIColor.white
  18. if #available(iOS 11.0, *) {
  19. self.navigationItem.searchController = searchController
  20. } else {
  21. // self.navigationItem.titleView = searchController.searchBar
  22. // Fallback on earlier versions
  23. }
  24.  
  25.  
  26.  
  27. extension ViewController: UISearchResultsUpdating {
  28. func updateSearchResults(for searchController: UISearchController) {
  29. let oldFilteredNames = self.filteredGuests
  30.  
  31. if (searchController.searchBar.text?.isEmpty)! {
  32. self.filteredGuests = guests
  33. } else {
  34. self.filteredGuests = guests.filter({ (user) -> Bool in
  35. return (user.name ?? "").lowercased().contains(searchController.searchBar.text!.lowercased()) || (user.surname ?? "").lowercased().contains(searchController.searchBar.text!.lowercased())
  36. })
  37. }
  38.  
  39. self.collectionView.performBatchUpdates({
  40. for (oldIndex, oldUser) in oldFilteredNames.enumerated() {
  41. if self.filteredGuests.contains(oldUser) == false {
  42. let indexPath = IndexPath(item: oldIndex, section: 0)
  43. self.collectionView.deleteItems(at: [indexPath])
  44. }
  45. }
  46.  
  47. for (index, name) in self.filteredGuests.enumerated() {
  48. if oldFilteredNames.contains(name) == false {
  49. let indexPath = IndexPath(item: index, section: 0)
  50. self.collectionView.insertItems(at: [indexPath])
  51. }
  52. }
  53. }, completion: nil)
  54. }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement