Created
May 10, 2019 02:21
-
-
Save GUIEEN/0cb92fa15effd68c018c5112e63ae1d4 to your computer and use it in GitHub Desktop.
ViewController implementation of searchbar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // ViewController.swift | |
| // SwiftSearch | |
| // | |
| // Created by Shrikar Archak on 2/16/15. | |
| // Copyright (c) 2015 Shrikar Archak. All rights reserved. | |
| // | |
| // Ref: https://shrikar.com/swift-ios-tutorial-uisearchbar-and-uisearchbardelegate/ | |
| import UIKit | |
| class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate{ | |
| @IBOutlet weak var searchBar: UISearchBar! | |
| @IBOutlet weak var tableView: UITableView! | |
| var searchActive : Bool = false | |
| var data = ["San Francisco","New York","San Jose","Chicago","Los Angeles","Austin","Seattle"] | |
| var filtered:[String] = [] | |
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| /* Setup delegates */ | |
| tableView.delegate = self | |
| tableView.dataSource = self | |
| searchBar.delegate = self | |
| } | |
| func searchBarTextDidBeginEditing(searchBar: UISearchBar) { | |
| searchActive = true; | |
| } | |
| func searchBarTextDidEndEditing(searchBar: UISearchBar) { | |
| searchActive = false; | |
| } | |
| func searchBarCancelButtonClicked(searchBar: UISearchBar) { | |
| searchActive = false; | |
| } | |
| func searchBarSearchButtonClicked(searchBar: UISearchBar) { | |
| searchActive = false; | |
| } | |
| func searchBar(searchBar: UISearchBar, textDidChange searchText: String) { | |
| filtered = data.filter({ (text) -> Bool in | |
| let tmp: NSString = text | |
| let range = tmp.rangeOfString(searchText, options: NSStringCompareOptions.CaseInsensitiveSearch) | |
| return range.location != NSNotFound | |
| }) | |
| if(filtered.count == 0){ | |
| searchActive = false; | |
| } else { | |
| searchActive = true; | |
| } | |
| self.tableView.reloadData() | |
| } | |
| override func didReceiveMemoryWarning() { | |
| super.didReceiveMemoryWarning() | |
| // Dispose of any resources that can be recreated. | |
| } | |
| func numberOfSectionsInTableView(tableView: UITableView) -> Int { | |
| return 1 | |
| } | |
| func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
| if(searchActive) { | |
| return filtered.count | |
| } | |
| return data.count; | |
| } | |
| func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { | |
| let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell; | |
| if(searchActive){ | |
| cell.textLabel?.text = filtered[indexPath.row] | |
| } else { | |
| cell.textLabel?.text = data[indexPath.row]; | |
| } | |
| return cell; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment