title: How to change background of a cell when the name contains the search bar text?
body: <p>I am trying to change the color of a cell if the name of the cell matches with part of the text in the search bar. Almost as the cell is being highlighted. I am using a search bar and a table view for this. </p>
  
  <p>I have tried this code with the help of the comments but I still can't figure it out:</p>
  
  <pre><code>let allElements = ["Hydrogen", "Helium", ...]
  
  var searchElement = [String]()
  var searching = false
  var myIndex = 0
  
  override func viewDidLoad() {
      super.viewDidLoad()
  
      // Do any additional setup after loading the view.
  }
  
  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
      print("row selected : \(indexPath.row)")
  
      if indexPath.row == 0 {
  
          let hydrogenSearchSegue = UIStoryboard(name:"Main", bundle:nil).instantiateViewController(withIdentifier: "hydrogenView") as! hydrogenViewController
  
          self.navigationController?.pushViewController(hydrogenSearchSegue, animated:true)
  
  
  
  
       }
  
  }
  
  }
  
  
  extension searchViewController: UITableViewDataSource, UITableViewDelegate {
  
  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
  
      if searching == true {
  
          return searchElement.count
  
      } else {
  
          return allElements.count
  
      }
  
  }
  
  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
  
      let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
  
      if searching == true {
  
          cell?.textLabel?.text = searchElement[indexPath.row]
  
      } else {
  
          cell?.textLabel?.text = allElements[indexPath.row]
  
      }
  
      return cell!
  
  }
  
  func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
  
      let string = allElements[indexPath.row]
  
      if searchElement.contains(string) {
  
          cell.backgroundColor = UIColor.red
  
      } else {
  
          cell.backgroundColor = UIColor.white
  
      }
  
  }
  
  }
  
  extension searchViewController: UISearchBarDelegate {
  
      func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
  
          searchElement = allElements.filter({$0.lowercased().prefix(searchText.count) == searchText.lowercased()})
          searching = true
          tableView.reloadData()
  
      }
  
      func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
  
          _ = navigationController?.popViewController(animated: true)
          searchBar.text = ""
          tableView.reloadData()
  
      }
  
  }
  </code></pre>
  
  <p>The goal was to try and use the .filter to separate the cells that don't contain the search bar text with what does. Then change the color of the cell if it does contain that text.</p>