こんにちは、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
.. に正常に入力されていますが、検索機能が機能していません