2

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

hellosheikh
  • 2,929
  • 8
  • 49
  • 115
  • Have your tried tableview.reloadData() in the end of your updateSearchResultsForSearchController method? THis will tell the table to reload and should use the new array. Also make sure the isActive method returns as expected – JDM Feb 17 '16 at 23:35
  • yes I did. but still it isn't working @JohnMejia – hellosheikh Feb 18 '16 at 06:52
  • Does filterTableData have records? Are you stepping through, is it going into your if(self.resultSearchController.active) condition?? – JDM Feb 18 '16 at 15:41
  • yes it is going but filterTableData has no records. its empty – hellosheikh Feb 19 '16 at 12:53

0 Answers0