Today we are going to share a simple task in swift 2.0. If you are a iOS learner and trying to make a simple search bar to explore data from table view then this is the perfect example for you. Here we are showing that how you can grab data in table view using search bar in swift 2.0.
import UIKit
classViewController:UIViewController,UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate{
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var searchBar: UISearchBar!
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()
tableView.delegate = self
tableView.dataSource = self
searchBar.delegate = self
self.tableView.backgroundColor = UIColor.lightGrayColor()
// Do any additional setup after loading the view, typically from a nib.
}
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, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var selectedCell:UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!
selectedCell.contentView.backgroundColor = UIColor.blueColor()
}
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;
}
Enjoy..!!
I must say you have high quality content here.
Your blog can go viral. You need initial boost only.
Greetings! Quite helpful guidance on this article! It is the little
changes that make the largest changes. Thanks a lot for sharing!
You should participate in a contest for one of the
finest sites online. I am going to recommend this website!
Great line up. We’ll be linking to this excellent article on our website.
Keep up the good writing.
Great post. I am confronting a couple of these problems.
Greetings! Quite helpful advice on this article!
It’s the little changes that make the largest changes.
Thanks a lot for sharing!
I wanted to thank you for this excellent read!! I undoubtedly appreciating every small bit of it I have you bookmarked to check out new stuff you post.
Good information. Blessed me I reach on your own website by accident, I
bookmarked it.
I adore it when people come together and share opinions,
great blog, keep it up.