重复发生在UISearchbar swift 5中

问题描述

我从移动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()
}

enter image description here

解决方法

您要更改行数吗?喜欢

sum