0

我有一个 UIViewController,它将另一个 UIViewController 呈现为 UISearchResultsController。

我希望这个 searchResultsController 在搜索时显示 GooglePlaces。

我在控制台中打印出所有自动完成预测,但是 UITableView 没有加载数据。

这是我的功能;

func updateSearchResults(for searchController: UISearchController) {
        searchController.searchResultsUpdater = self
        if searchController.isActive {
            searchController.searchResultsController?.view.isHidden = false
        }
        if searchController.searchBar.text == "" {
            self.searchResults.removeAll()
        } else {
            guard let query = searchController.searchBar.text else { return }
            GMSPlacesClient.shared().autocompleteQuery(query, bounds: nil, filter: filteredResults) { (predictions, error) in
                if error != nil {
                    print(error as Any)
                    return
                } else {
                    guard let searchPredictions = predictions else { return }
                    self.searchResults = searchPredictions
                    print("PREDICTION: \(searchPredictions)")
                    DispatchQueue.main.async {
                        self.resultsTableView.reloadData()
                    }
                }
            }
        }
    }

卫生检查

  1. 表视图委托
  2. 表视图数据源
  3. 预测
  4. numberOfRowsInSection

1 和 2 设置为自身。

3是印刷;

GMSAutocompletePrediction 0x6000037f0450: "England Street, Charlotte, NC, USA", id: EiJFbmdsYW5kIFN0cmVldCwgQ2hhcmxvdHRlLCBOQywgVVNB, types: (
    route,
    geocode

4 给了我正确数量的预测

我在这里关注谷歌的自定义实现;

https://developers.google.com/places/ios-sdk/autocomplete#get_place_predictions_programmatically

在“以编程方式获取地点预测”下

一如既往的任何帮助表示赞赏。

更新:TableView 方法

extension SearchResultsController: UITableViewDelegate, UITableViewDataSource {

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return searchResults.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let resultsCell = resultsTableView.dequeueReusableCell(withIdentifier: resultsCellIdentifier, for: indexPath) as! ResultsCell
    let resultsAttributedFullText = searchResults[indexPath.row].attributedFullText.string
    resultsCell.textLabel?.text = resultsAttributedFullText
    return resultsCell
}

更新:cellForRowAt

在进一步检查我的 cellForRowAt 函数后;

let results = searchResults[indexPath.row].placeID

打印results到控制台不会给我任何输出。

更新:

在处添加断点结果self.searchResults = searchPredictions

断点打印

更新:

在控制台中,我收到以下错误;

[BoringSSL] nw_protocol_boringssl_get_output_frames(1300) [C3.1:2][0x7ffd627057e0] get output frames failed, state 8196

不确定这是否与我的问题有关,但无论如何都要注意。

4

1 回答 1

0

您可以替换以下内容:

DispatchQueue.main.async {
     self.resultsTableView.reloadData()
}

DispatchQueue.main.async {
     searchController.searchResultsController.resultsTableView.reloadData()
}
于 2018-07-24T14:09:54.850 回答