title: UISearchController results TableViewController doesn't clear results when UISearchBar is empty
body: <p>I'm not great at iOS development and stuck on a simple problem.</p>
  
  <p>I'm trying to create a UISearchController that has an empty message in the backgroundView of the results tableView when it's first launched or when the UISearchBar is empty (whether from backspacing, tapping the x to clear the field, or cancelling out of the UISearchBar).</p>
  
  <p>I've accomplished adding the empty message when the UISearchBar is first focused, but after search has occurred, old search results are still in the results table view after I cancel out of search and when I empty the UISearchBar.</p>
  
  <p>I think this has always been the case, but I haven't been seeing the old results previously, whereas now I set <code>searchController.searchResultsController?.view.isHidden = false</code> to be able to see the background when the controller is first launched.</p>
  
  <p>I've searched pretty extensively and just can't figure out how to clear the old results.</p>
  
  <p>My whole results TableViewController code:</p>
  
  <pre><code>import UIKit
  import MapKit
  import Mapbox
  
  class LocationSearchTableViewController: UITableViewController {
  
      var matchingItems: [MKMapItem] = []
      var mapView: MGLMapView? = nil
  
      // This somehow connects and pleases the delegate
      var handleMapSearchDelegate: HandleMapSearch? = nil
  
      // Address formatting, not entirely sure what this is doing
      func parseAddress(selectedItem: MKPlacemark) -&gt; String {
          // put a space between "4" and "Melrose Place"
          let firstSpace = (selectedItem.subThoroughfare != nil &amp;&amp; selectedItem.thoroughfare != nil) ? " " : ""
          // put a comma between street and city/state
          let comma = (selectedItem.subThoroughfare != nil || selectedItem.thoroughfare != nil) &amp;&amp; (selectedItem.subAdministrativeArea != nil || selectedItem.administrativeArea != nil) ? ", " : ""
          // put a space between "Washington" and "DC"
          let secondSpace = (selectedItem.subAdministrativeArea != nil &amp;&amp; selectedItem.administrativeArea != nil) ? " " : ""
          let addressLine = String(
              format:"%@%@%@%@%@%@%@",
              // street number
              selectedItem.subThoroughfare ?? "",
              firstSpace,
              // street name
              selectedItem.thoroughfare ?? "",
              comma,
              // city
              selectedItem.locality ?? "",
              secondSpace,
              // state
              selectedItem.administrativeArea ?? ""
          )
          return addressLine
      }
  }
  
  // Extension with UISearchResultsUpdating delegate protocols
  extension LocationSearchTableViewController: UISearchResultsUpdating {
      func updateSearchResults(for searchController: UISearchController) {
  
          // This unhides the results view so it's visible when the field is focused
          searchController.searchResultsController?.view.isHidden = false
  
          // Get searchbar text and make query
          guard let _ = mapView,
              let searchBarText = searchController.searchBar.text else { return }
          let request = MKLocalSearchRequest()
          request.naturalLanguageQuery = searchBarText
          let search = MKLocalSearch(request: request)
  
          search.start { response, _ in
              guard let response = response else {
                  return
              }
              self.matchingItems = response.mapItems
              self.tableView.reloadData()
          }
  
          if searchBarText == "" {
  //            searchResults.removeAll()
              tableView.reloadData()
          }
      }
  }
  
  // Extension with all UITableViewDataSource methods grouped together
  extension LocationSearchTableViewController {
  
      // Create rows based on number of returning items
      override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
          if matchingItems.count == 0 {
              setEmptyMessage()
          } else {
              restore()
          }
  
  
          return matchingItems.count
      }
  
  
      // Populate cells
      override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
          let cell = tableView.dequeueReusableCell(withIdentifier: "cell")!
          let selectedItem = matchingItems[indexPath.row].placemark
          cell.textLabel?.text = selectedItem.name
          cell.detailTextLabel?.text = parseAddress(selectedItem: selectedItem)
  
          return cell
      }
  }
  
  // Groups UITableViewDelegate methods together
  extension LocationSearchTableViewController {
  
      // Function that fires on tapping a row
      override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  
          // Assign placemark value (coordinates)
          let selectedItem = matchingItems[indexPath.row].placemark
  
  
          // Delegate function that passes placemark
          handleMapSearchDelegate?.dropPin(placemark: selectedItem)
  
          // Dismiss searchController
          dismiss(animated: true, completion: nil)
      }
  }
  
  extension LocationSearchTableViewController {
  
      func setEmptyMessage() {
          let rect = CGRect(origin: CGPoint(x: 0,y :0), size: CGSize(width: self.view.bounds.size.width, height: self.view.bounds.size.height))
          let messageLabel = UILabel(frame: rect)
          messageLabel.text = "Empty"
          messageLabel.textColor = .black
          messageLabel.numberOfLines = 0
          messageLabel.textAlignment = .center
          messageLabel.font = UIFont(name: "TrebuchetMS", size: 15)
          messageLabel.sizeToFit()
  
          self.tableView.backgroundView = messageLabel
          //            self.tableView.backgroundView?.isHidden = false
          self.tableView.backgroundColor = Colors.black1
          self.tableView.separatorStyle = .none
      }
  
      func restore() {
          self.tableView.backgroundView = nil
          self.tableView.separatorStyle = .singleLine
      }
  }
  </code></pre>
  
  <p>And the relevant (I think) UISearchBar code from the main ViewController which implements the UISearchController:</p>
  
  <pre><code>func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
  
          if let navigationController = navigationController { // this unwraps navigation controller
              navigationController.setNavigationBarHidden(true, animated: true)
              UIView.animate(withDuration: 1) {
                  self.placeholderView.alpha = 0
              }
          }
      }
  </code></pre>