你好,我的搜索功能有一个非常奇怪的问题。我已经成功实现了搜索功能。我从后端服务获取数据。问题是在某个阶段,根据键入的关键字,数据无法在建议区域(tableView)中准确加载。我也在控制台上打印结果以检查我是否正在获得关键字的准确结果,并且控制台显示准确的结果,只是建议区域有时不会加载确切的结果。例如在我的应用程序中如果我想搜索城市“拉合尔”。我输入了完整的字母“拉合尔”
但是当我按x图标或退格键删除“e”时,它会显示准确的结果
我只是作为一个例子展示它。这种情况几乎一直都在发生。你能看看我的代码,看看我做错了什么。
class CountryTableViewController: UITableViewController, UISearchResultsUpdating {
var dict = NSDictionary()
var filteredKeys = [String]()
var resultSearchController = UISearchController()
var newTableData = [String]()
override func viewDidLoad() {
super.viewDidLoad()
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.filteredKeys.count
} else {
return dict.count
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CountryTableViewCell
if(self.resultSearchController.active){
let cityName = (((self.dict["\(indexPath.row)"] as?NSDictionary)!["Country"] as?NSDictionary)!["city_name"] as?NSString)
let stateName = (((self.dict["\(indexPath.row)"] as?NSDictionary)!["Country"] as? NSDictionary)!["state_name"] as? NSString)
let shortName = (((self.dict["\(indexPath.row)"] as?NSDictionary)!["Country"] as? NSDictionary)!["short_country_name"] as? NSString)
if (cityName != "-" || shortName != "-"){
cell.stateNameLabel.text = stateName as? String
cell.cityNameLabel.text = cityName as? String
cell.shortNameLabel.text = shortName as? String
}
return cell
}else{
if let cityName = (((self.dict["\(indexPath.row)"] as?NSDictionary)!["Country"] as?NSDictionary)!["city_name"] as?NSString){
cell.cityNameLabel.text = cityName as String
}
return cell
}
}
func updateSearchResultsForSearchController(searchController: UISearchController) {
let searchWord = searchController.searchBar.text!
getCountriesNamesFromServer(searchWord)
self.filteredKeys.removeAll()
for (key, value) in self.dict {
let valueContainsCity: Bool = (((value as? NSDictionary)?["Country"] as? NSDictionary)?["city_name"] as? String)?.uppercaseString.containsString(searchWord.uppercaseString) ?? false
let valueContainsCountry: Bool = (((value as? NSDictionary)?["Country"] as? NSDictionary)?["country_name"] as? String)?.uppercaseString.containsString(searchWord.uppercaseString) ?? false
if valueContainsCity || valueContainsCountry{ self.filteredKeys.append(key as! String) }
}
self.tableView.reloadData()
}
func getCountriesNamesFromServer(searchWord:String){
let url:String = "http://localhost"
let params = ["keyword":searchWord]
ServerRequest.postToServer(url, params: params) { result, error in
if let result = result {
print(result)
self.dict = result
}
}
}
}
答案 0 :(得分:2)
您在请求启动后重新加载表,而不是在完成时,因此您的dict仍然具有上次运行查询的结果。
执行updateSearchResults..
getCountriesNamesFromServer
方法中的所有内容移至您的网络请求的完成处理程序中
您的新方法将是:
self.dict = result