How to Implement UISearchBar in iPhone App For Data Searching

ImplementUIsearchbar in iphone

Recently, we’ve been getting a lot of requests from our blog subscribers to share the implementation process of UISearchBar to provide data searching feature in iPhone app.

While it’s not that difficult task to add search bar in an iPhone app, but today we’ll walk through its concept and implementation process in our iOS app tutorial.

UISearchBar Example

Today, we’ll create a sample app to demonstrate how to add search bar in any iOS app by using UITableView. We’ll add the list of country names, so that users can easily country name-list on searching an alphabet or phrase through UI SearchBar.

Let’s Get Started

Open your XCode and create a new project under file menu.


Select Single View Application and click on Next.


In the next tab, write down project name and description.


Once you create a new project, go to Main.storyboard and select ViewController.


Now drag and drop UISearchBar from “Show the object library” section.



Now select UISearchBar and see the attribute inspector which have properties of UISearchBar. We can manage the text, placeholder, bar style, background, keyboard functions like appearance, return key, spell checking etc.


Now, we set outlet UI SearchBar in ViewController as well as UITableView for show the list and get the search result after filtration.

@IBOutlet weak var searchBar: UISearchBar!
@IBOutlet weak var tblSearch: UITableView!



Now set delegate for UISearchBar and UITableView.

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate

override func viewDidLoad() {
self.tblSearch.dataSource = self
self.tblSearch.delegate = self
self.searchBar.delegate = self
// Do any additional setup after loading the view, typically from a nib.

Now, we load all the country list in table by UITableView datasource and delegate.

//MARK: UITableViewDataSource

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if(isSearch) {
return arrFilter.count
return arrCountry.count;

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
configureCell(cell, forRowAtIndexPath: indexPath)
return cell

func configureCell(cell: UITableViewCell, forRowAtIndexPath: NSIndexPath) {
cell.textLabel?.text = arrFilter[forRowAtIndexPath.row]
} else {
cell.textLabel?.text = arrCountry[forRowAtIndexPath.row];

Now, we add UI SearchBar delegate.

//MARK: UISearchbar delegate
func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
isSearch = true;

func searchBarTextDidEndEditing(searchBar: UISearchBar) {
isSearch = false;

func searchBarCancelButtonClicked(searchBar: UISearchBar) {
isSearch = false;

func searchBarSearchButtonClicked(searchBar: UISearchBar) {
isSearch = false;

func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {

if searchText.characters.count == 0 {
isSearch = false;
} else {
arrFilter = arrCountry.filter({ (text) -> Bool in
let tmp: NSString = text
let range = tmp.rangeOfString(searchText, options: NSStringCompareOptions.CaseInsensitiveSearch)
return range.location != NSNotFound
if(arrFilter.count == 0){
isSearch = false;
} else {
isSearch = true;

And done!


Now when a user will start a search, the UI SearchBar will superimpose the search bar interface and display the search results.

But, if you’re not a technical person, then the implementation process could become daunting task. And, if your desire is to add this feature in your iPhone app or build an iPhone app from ground, hire best iPhone app development company, discuss your app idea, and get started working on your app as soon as possible.

Grab a free copy of search bar example demo from Github.

You may also like,

This page was last edited on November 13th, 2020, at 10:35.

Author Bio

Hitesh Trivedi

Hitesh Trivedi

Designation: iOS Team Lead

Hitesh Trivedi is an iOS Team Lead at Space-O Technologies. He has over 9 years of experience in iOS app development. He has guided to develop over 100 iPhone apps with unique features and functionalities. He has special expertise in Swift and Objective-C.


Have an iOS App Idea in Mind? Let's Discuss!

Get your free consultation now

One thought on “How to Implement UISearchBar in iPhone App For Data Searching

  1. Kiran

    when I search “India” it was ok but If I was search “Indiana” it will show full list but I want list should be blank if I search any name that will not present in that country array.Please help with the solution?


Leave a Reply

Your email address will not be published. Required fields are marked *