我正在构建个人天气应用程序,用户可以在其中选择城市并显示天气温度。
我在此 JSON 文件中使用搜索:
我使用以下代码进行搜索:
搜索扩展:
extension SearchUI: UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {
filterContentForSearchText(searchText: searchController.searchBar.text!)
}
过滤功能:
func filterContentForSearchText(searchText: String) {
filteredCountries = countries.array!.filter { country in
return country["name"].stringValue.lowercased().hasPrefix(searchText.lowercased())
}
tableView.reloadData()
}
我的 cellForRowAt 函数:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell : UITableViewCell! = tableView.dequeueReusableCell(withIdentifier: "countryCell")
var data: JSON
if searchController.isActive && searchController.searchBar.text != "" {
data = filteredCountries[indexPath.row]
} else {
data = countries[indexPath.row]
}
let countryName = data["country"].stringValue
let countrycity = data["name"].stringValue
//data1.append(data["name"].stringValue)
cell?.textLabel?.text = countrycity
cell?.detailTextLabel?.text = countryName
return cell!
}
我的应用程序中的搜索过程非常慢,并且在过滤过程中键盘输入速度很慢。
通知: 国家 - 是一个 JSON 对象
谢谢大家!
最佳答案
解析:
countries = try! JSON(data: NSData(contentsOfFile: Bundle.main.path(forResource: "cities_with_countries", ofType: "json")!)! as Data)
关于swift - 提高搜索速度结果 - swift tableviewcontroller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44433194/