I am implementing a Places autocomplete with the Google Places API for iOS. Using Swift. My code for the searchview is the following (connected to an empty ViewController in the storyboard):
import UIKit
import GooglePlaces
protocol PlacesProtocol: class {
func returnPlaces(place:GMSPlace!)
}
class AutoCompleteViewController: UIViewController {
var resultsViewController: GMSAutocompleteResultsViewController?
var searchController: UISearchController?
var resultView: UITextView?
var delegate: PlacesProtocol?
override func viewDidLoad() {
super.viewDidLoad()
resultsViewController = GMSAutocompleteResultsViewController()
resultsViewController?.delegate = self
searchController = UISearchController(searchResultsController: resultsViewController)
searchController?.searchResultsUpdater = resultsViewController
let subView = UIView(frame: CGRectMake(0, 65.0, 375.0, 45.0))
subView.addSubview((searchController?.searchBar)!)
self.view.addSubview(subView)
searchController?.searchBar.sizeToFit()
searchController?.hidesNavigationBarDuringPresentation = false
// When UISearchController presents the results view, present it in
// this view controller, not one further up the chain.
self.definesPresentationContext = true
}
}
// Handle the user's selection.
extension AutoCompleteViewController: GMSAutocompleteResultsViewControllerDelegate {
func resultsController(resultsController: GMSAutocompleteResultsViewController,
didAutocompleteWithPlace place: GMSPlace) {
searchController?.active = false
// Do something with the selected place.
delegate!.returnPlaces(place)
self.navigationController?.popViewControllerAnimated(true)
}
func resultsController(resultsController: GMSAutocompleteResultsViewController,
didFailAutocompleteWithError error: NSError){
// TODO: handle the error.
print("Error: ", error.description)
}
(Taken from a tutorial)
The autocomplete works fine, but I need to add a static cell to the resultsTable with the option 'My Location'. Is there a way to access this? I am thinking either accessing the resultTable's cellForRowAtIndexPath function and adding an extra cell there, or accessing the array of places and putting in the option in the first position (more hacky), but I cant seem to figure out how and Google isn't giving me any results..
Thank you so much for any help!
Copyright Notice:Content Author:「AkkaVoskuil」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/40901287/adding-a-custom-cell-to-google-places-autocomplete-with-swift