您好我正在尝试实现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中删除该对象。我知道在Array中我们这样做
filterTableData.removeAll(keepCapacity: false)
我想NSDictionary
我必须使用NSMUtableDictionary
但我不知道这是不是问题。如果是问题仍然不知道如何从NSMutableDictionary
中删除对象,简而言之,以使此搜索功能可行。
注意:数据在tableView
中成功填充..只是搜索功能无效