2

您好,我正在尝试实现 SearchBarController,但是如果我在搜索栏中输入内容,则不会根据输入的字母出现。这是我的代码

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)

    }

我认为问题出在这个函数中updateSearchResultsForSearchController,因为首先我认为我必须从 Nsdictionary 中删除该对象。我知道在数组中我们这样做

 filterTableData.removeAll(keepCapacity: false)

我认为NSDictionary我必须使用NSMUtableDictionary但我不知道这是否是问题所在。如果是问题仍然不知道如何从中删除对象NSMutableDictionary,简而言之以使此搜索功能可行。

注意:数据正在成功tableView填充..只是搜索功能不起作用

4

0 回答 0