How To Search Data In Table View Using Swift 2.0

Inwizards-blog-img

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..!!

How To Search Data In Table View Using Swift 2.0

Post navigation


0 0 vote
Article Rating
Subscribe
Notify of
guest
9 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
98Andrea
98Andrea
7 years ago

I must say you have high quality content here.
Your blog can go viral. You need initial boost only.

Nola
Nola
7 years ago

Greetings! Quite helpful guidance on this article! It is the little
changes that make the largest changes. Thanks a lot for sharing!

Luther
Luther
7 years ago

You should participate in a contest for one of the
finest sites online. I am going to recommend this website!

Julia
Julia
7 years ago

Great line up. We’ll be linking to this excellent article on our website.
Keep up the good writing.

Keira
Keira
7 years ago

Great post. I am confronting a couple of these problems.

Brenda
Brenda
7 years ago

Greetings! Quite helpful advice on this article!

It’s the little changes that make the largest changes.
Thanks a lot for sharing!

Jonnie
Jonnie
7 years ago

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.

Lilla
Lilla
7 years ago

Good information. Blessed me I reach on your own website by accident, I
bookmarked it.

Dexter
Dexter
7 years ago

I adore it when people come together and share opinions,
great blog, keep it up.

9
0
Would love your thoughts, please comment.x
()
x