0

I am using Google AutoComplete to search Addresses. I am getting a result in the console, however, this is not populating into the TableView.

  • I am in the main queue when reloading the table.
  • I am getting a print count of the number of objects in the result.

SearchResultsController

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()
    }
}

TableView

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)
    }

Console Output

[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
)]

Any help greatly appreciated.

Update

Break point output on .reloadData()

Above is the breakpoint on .reloadData() on the tableView.
Therefore, the table is being reloaded and the array is being populated with the results. The issue is cellForRowAtIndex isnt being called for some reason!?

David Henry
  • 1,972
  • 20
  • 43

1 Answers1

-1

You do not need to add your tableview Please check below code

import GooglePlaces
import GoogleMaps

class DirectionsViewController: UIViewController, GMSAutocompleteViewControllerDelegate {

    let filter = GMSAutocompleteFilter()

    override func viewDidLoad() {
        super.viewDidLoad()
        txfFromLocation.addTarget(self, action: #selector(autocompleteClicked), for: .editingDidBegin)
    }

    @objc func autocompleteClicked (sender:UITextField!){
        let autocompleteController = GMSAutocompleteViewController()
        autocompleteController.delegate = self
        present(autocompleteController, animated: true, completion: nil)
    }

    //MARK:- GMSAUTOCOMPLETEDELEGATES
    // Handle the user's selection.
    func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
        print("Place name: \(place.name)")
        dismiss(animated: true, completion: nil)
    }
}
Yogesh Tandel
  • 1,738
  • 1
  • 19
  • 25