Выберите ячейку из SearchBar в Swift - PullRequest
0 голосов
/ 02 августа 2020

Я пытаюсь получить информацию из ячейки, предоставленной во время поиска, и вернуть ее предыдущему контроллеру представления. Вот мой код:

   //variables:
   var searching = false
   var cityArray = ["New York City, NY", "LA, CA"]
   var searchCity = [String()]
   
  //setup
  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
     if searching {
        return searchCity.count
    } else {
      return cityArray.count
 }
  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  let cell = tableView.dequeueReusableCell(withIdentifier: "CityTableViewCell", for: indexPath) as! 
  CityTableViewCell 
  if searching {
   cell.cityLabel.text = searchCity[indexPath.row]
  } else {
  cell.cityLabel.text = cityArray[indexPath.row]
  } 
  return cell 
 }



    //tells what do once selecting the tableviewcell
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if searching {
     //This is the problem. I am telling to take the search city IndexPath.row
     currentCity = searchCity[indexPath.row]
    } else {
     currentCity = cityArray[indexPath.row]
   }  
    //currentCity is then used to go back to prev. VC and tell it what city was selected
   if let presentingVC = presentingViewController as? DealsViewController {
        if presentingVC.city != self.currentCity {
            presentingVC.city = self.currentCity
            presentingVC.deleteAllDeals()
            presentingVC.noDeals = false
            presentingVC.cityOutlet.setTitle(self.currentCity,for: .normal)
        DispatchQueue.main.async {
                presentingVC.getDealInfo {
                presentingVC.DealsTableView.reloadData()
                presentingVC.DealsTableView.layoutIfNeeded()
                }
            }
        }
        
    }
    searchBar.text = currentCity
    searching = false
    tableView.deselectRow(at: indexPath, animated: true)
    self.dismiss(animated: true, completion: nil)
    searchBar.resignFirstResponder()
 }

//searchBar

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    searchCity = cityArray.filter({$0.prefix(searchText.count) == searchText})
    searching = true
    reloadData()
}

К сожалению, если он находится в режиме поиска, возвращаемый город не меняется. Это то же самое, что и в предыдущем контроллере представления. Я хочу получить точный indexpath.row из searchCity.

Спасибо!

1 Ответ

0 голосов
/ 02 августа 2020

Разобрался:

  1. Произошла ошибка транскрипции

  2. , чтобы изменить его на нижний регистр: searchCity = cityArray.filter ({$ 0 .lowercased (). prefix (searchText.count) == searchText.lowercased ()})

Спасибо!

...