尝试复制 GMSPlacesClient:autoCompleteQuery 函数以手动填充 UITableView
Attempting to replicate GMSPlacesClient: autoCompleteQuery function to populate a UITableView manually
我当前使用 Google Places API 端点的地址填充 tableView 的代码如下:
但是,我想创建一个自定义客户端来处理由 Places 提供的 autoCompleteQuery 手动处理的功能。我假设这需要再次解析地址 JSON 并进行迭代,然后将其存储在数组中。如果您有解决方案,请告诉我。注释代码完美运行,我正在尝试手动实现相同的结果。
func searchBar(searchBar: UISearchBar, textDidChange searchText: String){
// let mapsClient = GMSPlacesClient()
// mapsClient.autocompleteQuery(searchText, bounds: nil, filter: nil {(results, error: NSError?) in
//
// self.resultsArray.removeAll()
//
// if results == nil{
// return
// }
//
// for result in results! {
//
// if let result = result as GMSAutocompletePrediction! {
// self.resultsArray.append(result.attributedFullText.string)
// }
// }
//
// self.searchResultsClient.reloadDataWithArray(self.resultsArray)
// }
gmsFetcher?.sourceTextHasChanged(searchText)
self.searchResultsClient.reloadDataWithArray(self.resultsArray)
print(resultsArray)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension MainMapViewController: GMSAutocompleteFetcherDelegate {
func didAutocompleteWithPredictions(predictions: [GMSAutocompletePrediction]) {
self.resultsArray.count + 1
let resultsStr = NSMutableString()
for prediction in predictions {
resultsStr.appendFormat("%@\n", prediction.attributedPrimaryText)
}
resultText?.text = resultsStr as String
self.resultsArray.append(resultsStr as String)
self.searchResultsClient.reloadDataWithArray(self.resultsArray)
}
func didFailAutocompleteWithError(error: NSError) {
resultText?.text = error.localizedDescription
}
}
您可以使用 GMSAutocompleteFetcher
,它将 autocompleteQuery 方法包装在 GMSPlacesClient
上。提取器限制请求,只返回最近输入的搜索文本的结果,不提供 UI 元素。
实现 GMSAutocompleteFetcher 的步骤:
- Implement the
GMSAutocompleteFetcherDelegate
protocol.
- Create a
GMSAutocompleteFetcher
object.
- Call
sourceTextHasChanged
on the fetcher as the user types.
- Handle predictions and errors using the
didAutcompleteWithPredictions
and didFailAutocompleteWithError
protocol methods.
可以在 Use the fetcher.
中找到演示使用提取器的实施步骤的示例代码
已解决:以下是我如何最终解决从 UISearchBar 更新文本时使用自动完成更新 UITableView 行的问题。
func searchBar(searchBar: UISearchBar, textDidChange searchText: String){
self.resultsArray.removeAll()
gmsFetcher?.sourceTextHasChanged(searchText)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
//Implement GMSAutoCompleteFetcherDelegate protocol to handle custom string prediction
extension MainMapViewController: GMSAutocompleteFetcherDelegate {
func didAutocompleteWithPredictions(predictions: [GMSAutocompletePrediction]) {
for prediction in predictions {
if let prediction = prediction as GMSAutocompletePrediction!{
self.resultsArray.append(prediction.attributedFullText.string)
}
}
self.searchResultsTable.reloadDataWithArray(self.resultsArray)
print(resultsArray)
}
func didFailAutocompleteWithError(error: NSError) {
resultText?.text = error.localizedDescription
}
}
我当前使用 Google Places API 端点的地址填充 tableView 的代码如下:
但是,我想创建一个自定义客户端来处理由 Places 提供的 autoCompleteQuery 手动处理的功能。我假设这需要再次解析地址 JSON 并进行迭代,然后将其存储在数组中。如果您有解决方案,请告诉我。注释代码完美运行,我正在尝试手动实现相同的结果。
func searchBar(searchBar: UISearchBar, textDidChange searchText: String){
// let mapsClient = GMSPlacesClient()
// mapsClient.autocompleteQuery(searchText, bounds: nil, filter: nil {(results, error: NSError?) in
//
// self.resultsArray.removeAll()
//
// if results == nil{
// return
// }
//
// for result in results! {
//
// if let result = result as GMSAutocompletePrediction! {
// self.resultsArray.append(result.attributedFullText.string)
// }
// }
//
// self.searchResultsClient.reloadDataWithArray(self.resultsArray)
// }
gmsFetcher?.sourceTextHasChanged(searchText)
self.searchResultsClient.reloadDataWithArray(self.resultsArray)
print(resultsArray)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension MainMapViewController: GMSAutocompleteFetcherDelegate {
func didAutocompleteWithPredictions(predictions: [GMSAutocompletePrediction]) {
self.resultsArray.count + 1
let resultsStr = NSMutableString()
for prediction in predictions {
resultsStr.appendFormat("%@\n", prediction.attributedPrimaryText)
}
resultText?.text = resultsStr as String
self.resultsArray.append(resultsStr as String)
self.searchResultsClient.reloadDataWithArray(self.resultsArray)
}
func didFailAutocompleteWithError(error: NSError) {
resultText?.text = error.localizedDescription
}
}
您可以使用 GMSAutocompleteFetcher
,它将 autocompleteQuery 方法包装在 GMSPlacesClient
上。提取器限制请求,只返回最近输入的搜索文本的结果,不提供 UI 元素。
实现 GMSAutocompleteFetcher 的步骤:
- Implement the
GMSAutocompleteFetcherDelegate
protocol.- Create a
GMSAutocompleteFetcher
object.- Call
sourceTextHasChanged
on the fetcher as the user types.- Handle predictions and errors using the
didAutcompleteWithPredictions
anddidFailAutocompleteWithError
protocol methods.
可以在 Use the fetcher.
中找到演示使用提取器的实施步骤的示例代码已解决:以下是我如何最终解决从 UISearchBar 更新文本时使用自动完成更新 UITableView 行的问题。
func searchBar(searchBar: UISearchBar, textDidChange searchText: String){
self.resultsArray.removeAll()
gmsFetcher?.sourceTextHasChanged(searchText)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
//Implement GMSAutoCompleteFetcherDelegate protocol to handle custom string prediction
extension MainMapViewController: GMSAutocompleteFetcherDelegate {
func didAutocompleteWithPredictions(predictions: [GMSAutocompletePrediction]) {
for prediction in predictions {
if let prediction = prediction as GMSAutocompletePrediction!{
self.resultsArray.append(prediction.attributedFullText.string)
}
}
self.searchResultsTable.reloadDataWithArray(self.resultsArray)
print(resultsArray)
}
func didFailAutocompleteWithError(error: NSError) {
resultText?.text = error.localizedDescription
}
}