我们可以使用 Google 的位置自动完成 api 来填充 swift 上的表格视图单元格吗?

Can we use Google's place autocomplete api to populate out tableview cells on swift?

我想知道我们如何使用 google place api 的过程,以便当我们在 UITextField 中输入文本时,表格视图会重新加载以显示自动完成结果。 Google 上给出的文档使用它自己的 API。我是 ios 开发的新手。谁能帮助我如何进行?到目前为止,我已经完成了 pod 设置等,我能够让用户在文本字段的委托方法中输入文本。

这是我的代码,我无法在我的表格视图中看到任何结果

import UIKit
import GooglePlaces

class ViewController: UIViewController {
  @IBOutlet weak var schoolTextField: UITextField!
  @IBOutlet weak var schooltableView: UITableView!

  var placesClient : GMSPlacesClient?
  var resultArray  = [String]()

  override func viewDidLoad() {
    super.viewDidLoad()
  }

  func placeAutocomplete(text:String) {
    let filter = GMSAutocompleteFilter()
    filter.type = .noFilter
    placesClient?.autocompleteQuery(text, bounds: nil, filter: filter, callback: {(results, error) -> Void in   //unable to enter in this block
    if let error = error {
      print("Autocomplete error \(error)")
      return
    }
    if let results = results {
     self.resultArray = [String]()
     for result in results {
      self.resultArray.append(String(describing: result.attributedFullText)) 
      print("Result \(result.attributedFullText) with placeID \(result.placeID)")
    }
  }
  self.schooltableView.reloadData()
  })
 }
}

extension ViewController:UITextFieldDelegate {
  func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    let currentText = textField.text ?? ""
    placeAutocomplete(text:currentText)
    return true
     }
   }

  extension ViewController:UITableViewDelegate,UITableViewDataSource {
  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return resultArray.count
  }

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)as! SchoolCell
    cell.schoolLabel.text = resultArray[indexPath.row]
    return cell
  }

  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    //do something, unable to reach here
  }
}

您忘记实例化 GMSPlacesClient 并在 viewDidLoad 中调用 UITextField 和 UITableView 的委托。

override func viewDidLoad() {
    super.viewDidLoad()

    self.placesClient = GMSPlacesClient()
    self.schoolTextField.delegate = self
    self.schooltableView.delegate = self
    self.schooltableView.dataSource = self
}