Swift 3.0 Autocomplete Address For Search Bar

2.3k views Asked by At

I am interested in using a tableView to list possible addresses based on inputs in the search bar. After selecting the cell that contains the address desired, the search bar text consists of the address, however I want the possible addresses (cells) to disappear. Does self.searchResultsTableView.reloadData() in didSelectRowAt clear all the cells or is there another command? I am not certain how to clear the cells after selecting the appropriate address without iterating and having the suggestion introduce more cells.

import UIKit
import MapKit

class SearchViewController: UIViewController {

@IBOutlet weak var searchBar: UISearchBar!
    var searchCompleter = MKLocalSearchCompleter()
    var searchResults = [MKLocalSearchCompletion]()
    var searchSource: [String]?



@IBOutlet weak var searchResultsTableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    searchCompleter.delegate = self
    searchBar.delegate = self
}

}


extension SearchViewController: UISearchBarDelegate {

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {

    searchCompleter.queryFragment = searchText
}
}

extension SearchViewController: MKLocalSearchCompleterDelegate {

func completerDidUpdateResults(_ completer: MKLocalSearchCompleter) {
    searchResults = completer.results
    searchResultsTableView.reloadData()
}

func completer(_ completer: MKLocalSearchCompleter, didFailWithError error: Error) {
    // handle error
}
}

extension SearchViewController: 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 searchResult = searchResults[indexPath.row]
    let cell = UITableViewCell(style: .subtitle, reuseIdentifier: nil)
    cell.textLabel?.text = searchResult.title
    cell.detailTextLabel?.text = searchResult.subtitle

    return cell
}
}

extension SearchViewController: UITableViewDelegate {

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)

    let completion = searchResults[indexPath.row]

    let searchRequest = MKLocalSearchRequest(completion: completion)
    let search = MKLocalSearch(request: searchRequest)
    search.start { (response, error) in
        let coordinate = response?.mapItems[0].placemark.coordinate
        print(String(describing: coordinate))
        print(response?.mapItems)
        self.searchBar.text = response?.mapItems[0].name
    }


    self.searchResultsTableView.reloadData()


}
}
1

There are 1 answers

0
Nirav D On BEST ANSWER

If you want to clear your tableView then you need to make your datasource array empty and then reload the tableView.

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)

    let completion = searchResults[indexPath.row]

    let searchRequest = MKLocalSearchRequest(completion: completion)
    let search = MKLocalSearch(request: searchRequest)
    search.start { (response, error) in
        let coordinate = response?.mapItems[0].placemark.coordinate
        print(String(describing: coordinate))
        print(response?.mapItems)
        self.searchBar.text = response?.mapItems[0].name
    }

    //Make empty your array ant then reload tableView
    searchResults = []
    self.searchResultsTableView.reloadData()
}