Повторение происходит в UISearchBar swift 5

#arrays #swift #filter #tableview #uisearchbar

#массивы #swift #Фильтр #просмотр таблицы #uisearchbar

Вопрос:

Я получаю все контакты из мобильного SDK и сохраняю их в Coredata при извлечении.Это показывает репутацию, ПОЧЕМУ????

мой контроллер представления поиска повторяет строку снова и снова, это мой код

  var searchedCountry = [String]()
 var searching = false

override func viewDidLoad() {
    super.viewDidLoad()
    
    searchbar.delegate = self
    searchbar.searchBarStyle = UISearchBar.Style.prominent
    searchbar.placeholder = " Search..."
    searchbar.isTranslucent = false
  

}

     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

       let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ContactsCell

    if searching {
        cell.name.text = searchedCountry[indexPath.row]
    } else {
        cell.name.text = NameArray[indexPath.row]
    }
  return cell
 }
      

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

    searchedCountry = NameArray.filter({$0.lowercased().prefix(searchText.count) == searchText.lowercased()})
    searching = true
    tableview.reloadData()

}

func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {

    searching = false
    searchBar.text = ""
    tableview.reloadData()
}
  

введите описание изображения здесь

Ответ №1:

вы меняете количество строк? Нравится

 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        searching ? searchedCountry.count : NameArray.count
    }