【问题标题】:Repeatation occurs in UISearchbar swift 5重复发生在 UISearchbar swift 5
【发布时间】:2020-08-26 11:16:04
【问题描述】:

我从移动 sdk 获取所有联系人,并在检索时将其保存在 Coredata 中。它显示声誉 WHY????

我的搜索视图控制器一次又一次地重复字符串 这是我的代码

 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()
}

【问题讨论】:

    标签: arrays swift filter tableview uisearchbar


    【解决方案1】:

    你改变行数吗?喜欢

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-03
      • 1970-01-01
      • 1970-01-01
      • 2019-04-07
      • 2014-09-29
      相关资源
      最近更新 更多