0
私は、ユーザーが都市を選んで天気の温度を表示できるパーソナル天気アプリを構築しています。検索速度の向上 - スウィフトtableviewcontroller
私は、この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!
}
私のアプリで検索処理が非常に遅く、Imは、フィルタリング時にキーボードによって遅い入力で直面。通知の
: 国は - JSONオブジェクトは
おかげで、あなたのすべてです!
あなたが国をどういう意味ですかは、JSONオブジェクトですか?どのように/あなたのJSONを正確に解析するのですか?そしてSwiftでJSONオブジェクトをどのように表現しますか? –
filterContentForSearchTextメソッドは、検索テキストが変更されるたびに呼び出され、全国データセットの別の検索を行ってから、テーブルビューを再読み込みします。すべてこれは時間がかかるので、キープレスが起こっている間はそれぞれのキープレスが押されます。どのように複雑にしたいのかによって、それを克服するためのさまざまなオプションがありますが、基本的にバックグラウンドスレッドで検索し、完了したらメインスレッドを更新することができます。あなたはそれを行う場合、前のものをキャンセルするか、次のものの前に終わることを確認する必要があります。 –
あなたは私にあなたが意味するもののいくつかの例を示すことができますか? –