Search Bar controller in TableView

144 Views Asked by At

Hello I am trying to implement a SearchBarController but If I type something in the search bar data doesn't comes up according to the letters typed. Here is my code

class  CountriesTableViewController: UITableViewController, UISearchResultsUpdating {
    var dict = NSDictionary() 

  var filterTableData =  NSDictionary()
    var resultSearchController = UISearchController()





    override func viewDidLoad() {
        super.viewDidLoad()

        getCountriesNamesFromServer()

        self.resultSearchController = ({

            let controller  = UISearchController(searchResultsController: nil)
            controller.searchResultsUpdater = self
            controller.dimsBackgroundDuringPresentation = false
            controller.searchBar.sizeToFit()
            self.tableView.tableHeaderView = controller.searchBar
            return controller


        })()

        self.tableView.reloadData()
    }

       override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {


        if(self.resultSearchController.active){

        return self.filterTableData.count-1
        }else {
        return dict.count-1
        }



    }



    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

         let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CountriesTableViewCell

         if(self.resultSearchController.active){
           // (cell.contentView.viewWithTag(1) as! UILabel).text = filterTableData[indexPath.row]

            cell.countryNameLabel.text = (((filterTableData["\(indexPath.item)"] as?NSDictionary)!["Countries"] as?NSDictionary)!["name"] as?NSString)! as String
            return cell

         }else{

         cell.countryNameLabel.text = (((dict["\(indexPath.item)"] as?NSDictionary)!["Countries"] as?NSDictionary)!["name"] as?NSString)! as String
            return cell
        }


}

    func updateSearchResultsForSearchController(searchController: UISearchController) {

        let keys: NSArray = dict.allKeys
        let filteredKeys: [String] = keys.filteredArrayUsingPredicate(NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text!)) as! [String]
       filterTableData = dict.dictionaryWithValuesForKeys(filteredKeys)

    }

I think Problem is in this function updateSearchResultsForSearchController because first I think I have to remove the object from Nsdictionary. I know in Array we do this

 filterTableData.removeAll(keepCapacity: false)

and I think for NSDictionary I have to use NSMUtableDictionary But I don't know if that is the problem. If it is the problem still don't know how can I remove objects from NSMutableDictionary and in short to make this search functionality workable.

Note: Data is Populating successfully in tableView.. just the search functionality is not working

0

There are 0 best solutions below