我正在使用 Google AutoComplete 来搜索地址。我在控制台中得到了一个结果,但是,这并没有填充到 TableView 中。
- 重新加载表格时,我在主队列中。
- 我得到了结果中对象数量的打印计数。
搜索结果控制器
extension ResultsController: UISearchResultsUpdating, UISearchControllerDelegate {
func updateSearchResults(for searchController: UISearchController) {
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchBar.autocorrectionType = .yes
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: self.filteredResults) { (results, error) in
if error != nil {
print(error as Any)
return
} else {
guard let results = results else { return }
self.searchResults.append(contentsOf: results)
print(self.searchResults)
}
}
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
func didDismissSearchController(_ searchController: UISearchController) {
searchResults.removeAll()
}
}
表视图
extension ResultsController: 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 = tableView.dequeueReusableCell(withIdentifier: resultsCellIdentifier, for: indexPath)
let resultsAttributedFullText = searchResults[indexPath.row].attributedFullText.string
resultsCell.textLabel?.text = resultsAttributedFullText
return resultsCell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
view.endEditing(true)
}
控制台输出
[GMSAutocompletePrediction 0x60000053d6e0: "Whittier Boulevard, Whittier, CA, USA", id: EiVXaGl0dGllciBCb3VsZXZhcmQsIFdoaXR0aWVyLCBDQSwgVVNBIi4qLAoUChIJ988tx8bTwoARp1jTzq6WINISFAoSCfeHelWG08KAEVokQMFHfagn, types: (
route,
geocode
), GMSAutocompletePrediction 0x60000053f150: "Whitehorse Road, Balwyn VIC, Australia", id: EiZXaGl0ZWhvcnNlIFJvYWQsIEJhbHd5biBWSUMsIEF1c3RyYWxpYSIuKiwKFAoSCREWtq2qQNZqEQJK4PmVuEWbEhQKEglDvWU1O0HWahEw2IwhdVYEBQ, types: (
route,
geocode
), GMSAutocompletePrediction 0x600000532280: "Whiteman Street, Southbank VIC, Australia", id: EilXaGl0ZW1hbiBTdHJlZXQsIFNvdXRoYmFuayBWSUMsIEF1c3RyYWxpYSIuKiwKFAoSCe96AVJUXdZqEeCPutYFxDBlEhQKEgkP4YsKRV3WahHA_owhdVYEBQ, types: (
route,
geocode
), GMSAutocompletePrediction 0x600000531d40: "Whitechapel Road, London, UK", id: EhxXaGl0ZWNoYXBlbCBSb2FkLCBMb25kb24sIFVLIi4qLAoUChIJtVKaXcwcdkgR-UK_lKqNnh0SFAoSCamRx0IRO1oCEXoliDJDoPjE, types: (
route,
geocode
), GMSAutocompletePrediction 0x600000532400: "Whitehouse Lane, Yeadon, Leeds, UK", id: EiJXaGl0ZWhvdXNlIExhbmUsIFllYWRvbiwgTGVlZHMsIFVLIi4qLAoUChIJX11vv3ZYeUgR7a_1SItTcAgSFAoSCb1BkiJ1WHlIEdu33wgccaPf, types: (
route,
geocode
)]
非常感谢任何帮助。
更新
上面是 tableView 上 .reloadData() 的断点。
因此,正在重新加载表,并用结果填充数组。问题是 cellForRowAtIndex 由于某种原因没有被调用!?