【问题标题】:Delete all the previous UITableViewCell Swift 3/4 iOS删除所有之前的 UITableViewCell Swift 3/4 iOS
【发布时间】:2017-09-19 07:30:18
【问题描述】:

我目前有一个 UITableViewController,它使用来自服务器的 JSON 响应填充,所有这些都是用 Alamofire 完成的。

我的 TableView 顶部有我的搜索栏,当我输入关键字时,服务器会使用与我的搜索相关的 JSON 响应。但是当我再次搜索时,仍然会出现之前的搜索结果。

我想知道如何从tableView中清除之前的搜索。

【问题讨论】:

  • 删除数组中的所有元素并在搜索文本更改时调用 reloadData()
  • 当您在输入一些文本后从服务器端获得响应时,您可以从阵列中删除所有对象。
  • 只需删除您的 json 数组并显示一些代码
  • 阅读本文档应该可以帮助您更好地理解和实施解决方案developer.apple.com/documentation/uikit/uisearchcontroller

标签: ios json swift uitableview alamofire


【解决方案1】:

正如我在评论中提到的,当用户更改 UISearch 栏中的文本时,只需清除用于显示 tableView 的数组并重新加载 tableView。假设我的数组名称是dataSource

extension ViewController : UISearchBarDelegate {
    func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
        self.dataSource?.removeAll()
        self.tableView.reloadData()
    }
}

如果您想在用户点击搜索按钮时清除之前的结果

func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
    self.dataSource?.removeAll()
    self.tableView.reloadData()
}

选择适合您需要的。基本思想是一样的。清除数组并重新加载就是这样:)

【讨论】:

  • @pablo-colson : 我很高兴能帮上忙 :) 请考虑接受答案,因为它对你有帮助 :)
猜你喜欢
  • 2017-02-27
  • 2014-08-30
  • 2022-11-07
  • 1970-01-01
  • 1970-01-01
  • 2015-08-14
  • 2021-11-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多