title: how to load data in table view result of search bar swift
body: <p>I want to display the search result of a SearchBar in a tableView and I do not know how.</p>
  
  <p><a href="https://i.stack.imgur.com/95AK6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/95AK6.png" alt="enter image description here"></a></p>
  
  <p>when i type a Destination in the TextField, everything is works just fine, but when i type in the SearchBar is not working:</p>
  
  <p><a href="https://i.stack.imgur.com/pJh3F.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pJh3F.png" alt="enter image description here"></a></p>
  
  <p>Here is how to call a func to search in [String]: </p>
  
  <pre><code>extension ViewController: UITextFieldDelegate {
  
  
  public func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -&gt; Bool {
  
      let substring = (textField.text! as NSString).replacingCharacters(in: range, with: string)
  
      DestinoP.searchDestinos(substring)
  
      return true
  
  }}
  </code></pre>
  
  <p>here is how to show the result:</p>
  
  <pre><code>extension ViewController: UITableViewDataSource {
  
  public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
  
      let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as UITableViewCell
      let index = indexPath.row as Int
  
      isSearching = true // this is just for SearchBar
      if isSearching {
          if let str = autoCompleteDestino[index].desDestino {
              cell.textLabel?.text = str
          }
      }
  
      return cell
  }}
  </code></pre>
  
  <p>extension ViewController: UITableViewDelegate {</p>
  
  <pre><code>public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
  
      if isSearching { // this is just for SearchBar
          return autoCompleteDestino.count
      }
  
      return autoCompleteDestino.count
  
  }
  
  public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  
      let selectedCell: UITableViewCell = tableView.cellForRow(at: indexPath)!
  
      destinoSearch.text = selectedCell.textLabel!.text!
      busquedaDestinosText.text = selectedCell.textLabel!.text!
  
  }}
  </code></pre>
  
  <p>this is how i try to show the result when i type in the SearchBar:</p>
  
  <pre><code>extension ViewController: UISearchBarDelegate {
  
  func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
  
      let stringOfUser = (busquedaDestinosText.text)
  
      DestinoP.searchDestinos(stringOfUser!)
  
      if  destinoSearch.text == "" {
          isSearching = false
          view.endEditing(true)
          tableView.reloadData()
          print(isSearching)
      } else {
          isSearching = true
          print(isSearching)
          print(autoCompleteDestino)
      }
  
  }}
  </code></pre>
  
  <p>Can help me? </p>