Saturday, 6 December 2014

Jogendra Singh

UITableview with UISearchBar in swift Language

Create UISearchBar with UITableView in swift language ( iOS8 ). 


In this tutorials we are going to learn how to work UISearchBar with UITableView in swift language .
        UISearchBar Class implements a simple a text field for searches . UISearchBar object does not perform direct we need to user UISearchBarDelegate for this and implement the action when text entered and button clicked.



First Need to create UITableView in swift  : -  How to create / make a simple tableview in iOS8 and swift

Now need to implement UISearchBar in this

   Create global objects  : -

    @IBOutlet var tableView:UITableView!   // This is for your tableview
    
    @IBOutlet var searchBarObj:UISearchBar!  // It's for your search bar object 
    
    var is_searching:Bool!   // It's flag for searching  
    var dataArray:NSMutableArray!  // Its data array for UITableview 
    var searchingDataArray:NSMutableArray! // Its data searching array that need for search result show 


Add Delegate of UITableView and UISearchbar

class ViewController: UIViewController ,UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate


Now initialise all array and search flag

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        is_searching = false
        dataArray = ["Apple", "Samsung", "iPHone", "iPad", "Macbook", "iMac" , "Mac Mini"]
        searchingDataArray = []
        self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
        
    }

Now we need to minor change in UITableViewDataSource functions


    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        if is_searching == true{
            return searchingDataArray.count
        }else{
            return dataArray.count  //Currently Giving default Value
        }
    }
    
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
        var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
        if is_searching == true{
            cell.textLabel.text = searchingDataArray[indexPath.row] as NSString
        }else{
            cell.textLabel.text = dataArray[indexPath.row] as NSString
        }
        return cell;
    }

Hows show in simulator now : -





Now need to implement UISearchBar Delegate function


    func searchBar(searchBar: UISearchBar, textDidChange searchText: String){
        if searchBar.text.isEmpty{
            is_searching = false
            tableView.reloadData()
        } else {
            println(" search text %@ ",searchBar.text as NSString)
            is_searching = true
            searchingDataArray.removeAllObjects()
            for var index = 0; index < dataArray.count; index++
            {
                var currentString = dataArray.objectAtIndex(index) as String
                if currentString.lowercaseString.rangeOfString(searchText.lowercaseString)  != nil {
                    searchingDataArray.addObject(currentString)
                    
                }
            }
            tableView.reloadData()
        }
      } 

After searching we can result showing according to our search 





Using this delegate function we can search text using UISearchBar   in UITableView

Download Sample Project from here : - UITableView with SearchBar xCode Project


Thanks for Visit  , Also can visit on My blog Facebook page iPhone & iPad Application Development Help World and also can visit Google+ 

Jogendra Singh

About Jogendra Singh -

I'm Founder of Jogendra.Com. I love to share my bright ideas with the whole blogging community. I'm a good iPhone Application Developer. Apart from Blogging, I love to play Cricket and mobile games (Clash of clans).

Subscribe to this Blog via Email :