Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class CollectionViewController: UIViewController ,UICollectionViewDelegate,UICollectionViewDataSource,UISearchControllerDelegate, UISearchBarDelegate, UISearchResultsUpdating{
- var items = [Food]()
- var filtered = [Food]()
- var searchActive : Bool = false
- let searchController = UISearchController(searchResultsController: nil)
- // var items = ["ASIAN","SEAFOOD","SUSHI","CAFE","Los Angeles","Austin","Seattle"]
- @IBOutlet var collectionViewController: UICollectionView!
- override func viewDidLoad() {
- super.viewDidLoad()
- // imageViewArray=[#imageLiteral(resourceName: "food18") ,#imageLiteral(resourceName: "food19"),#imageLiteral(resourceName: "food20"),#imageLiteral(resourceName: "food21"),#imageLiteral(resourceName: "food22"),#imageLiteral(resourceName: "food23"),#imageLiteral(resourceName: "food24")]
- self.items.append(Food(title: "View Appointment", image: UIImage(named: "view appointments@1x.png")))
- self.items.append(Food(title: "Medical Chart", image:UIImage(named: "medical chart@1x.png")))
- self.items.append(Food(title: "Task", image:UIImage(named: "tasks@2x.png")))
- self.items.append(Food(title: "Referrals", image:UIImage(named: "referrals@2x.png")))
- self.items.append(Food(title: "Profile", image:UIImage(named: "profile@2x.png")))
- self.items.append(Food(title: "Wallet", image:UIImage(named: "wallet@2x.png")))
- collectionViewController.dataSource=self
- collectionViewController.delegate=self
- self.searchController.searchResultsUpdater = self
- self.searchController.delegate = self
- self.searchController.searchBar.delegate = self
- self.searchController.hidesNavigationBarDuringPresentation = false
- self.searchController.dimsBackgroundDuringPresentation = true
- self.searchController.obscuresBackgroundDuringPresentation = false
- searchController.searchBar.placeholder = "Search for tools and resources"
- searchController.searchBar.sizeToFit()
- searchController.searchBar.becomeFirstResponder()
- self.navigationItem.titleView = searchController.searchBar
- // Do any additional setup after loading the view, typically from a nib.
- }
- func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
- if searchActive {
- return filtered.count
- }
- else
- {
- return items.count //return number of rows in section
- }
- }
- func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
- let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CategoriesCollectionViewCell
- //configureCell(cell: cell as! toolCollectionViewCell, forItemAtIndexPath: indexPath as NSIndexPath)
- cell.imageViewCell=cell.viewWithTag(1) as! UIImageView
- if searchActive {
- cell.imageViewCell.image = self.filtered[indexPath.row].image
- cell.categoriesTitleLbl.text = self.filtered[indexPath.row].title
- } else {
- cell.imageViewCell.image = self.items[indexPath.row].image
- cell.categoriesTitleLbl.text = self.items[indexPath.row].title
- }
- return cell //return your cell
- }
- func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
- searchActive = false
- self.dismiss(animated: true, completion: nil)
- }
- func updateSearchResults(for searchController: UISearchController)
- {
- guard let searchString = searchController.searchBar.text else {
- return
- }
- filtered = items.filter({ (item) -> Bool in
- if let title = item.title {
- return title.lowercased().contains(searchString.lowercased())
- } else {
- return false
- }
- })
- collectionViewController.reloadData()
- }
- func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
- searchActive = true
- collectionViewController.reloadData()
- }
- func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
- searchActive = false
- collectionViewController.reloadData()
- }
- func searchBarBookmarkButtonClicked(_ searchBar: UISearchBar) {
- if !searchActive {
- searchActive = true
- collectionViewController.reloadData()
- }
- searchController.searchBar.resignFirstResponder()
- }
- }
- class Food {
- var title: String?
- var image: UIImage?
- init(title: String?, image: UIImage?) {
- self.title = title
- self.image = image
- }
- }
- class CategoriesCollectionViewCell: UICollectionViewCell {
- @IBOutlet weak var imageViewCell: UIImageView!
- @IBOutlet weak var categoriesTitleLbl: UILabel!
- }
Add Comment
Please, Sign In to add comment