Swift2 Swift-清除表格视图
我有一个下载JSON文件并在TableView中显示结果的搜索。如果用户搜索某个内容并获得结果列表,则搜索其他内容并返回0个结果。第一组结果仍在TableView中。我想在每次新搜索开始时清除TableView,以防止这种情况发生。我已经尝试将数据源设置为nil并重新加载TableView,但它不起作用。以下是我所拥有的:Swift2 Swift-清除表格视图,swift2,Swift2,我有一个下载JSON文件并在TableView中显示结果的搜索。如果用户搜索某个内容并获得结果列表,则搜索其他内容并返回0个结果。第一组结果仍在TableView中。我想在每次新搜索开始时清除TableView,以防止这种情况发生。我已经尝试将数据源设置为nil并重新加载TableView,但它不起作用。以下是我所拥有的: var searchResults : [[String : AnyObject]]? = nil func searchBarSearchButtonClicked(se
var searchResults : [[String : AnyObject]]? = nil
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
print("DEBUG: searchBarSearchButtonClicked")
if searchBar.text > "" {
//--- This isn't working ---
searchResults = nil
tableView.reloadData()
//--------------------------
activityIndicator.startAnimating()
dbSearcher.startSearch(searchBar.text!) { (results) -> () in
self.searchResults = results
self.activityIndicator.stopAnimating()
self.tableView.reloadData()
}
searchBar.endEditing(true)
}
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if searchResults != nil {
print("DEBUG: Result count \(searchResults!.count)")
return searchResults!.count
} else {
print("DEBUG: Result count 0") //I don't see this other than when the ViewController first loads
return 0
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier("searchResult")!
cell.textLabel?.text = searchResults![indexPath.row]["Title"] as? String
cell.detailTextLabel?.text = searchResults![indexPath.row]["Year"] as? String
//Display cover
let imageURL = searchResults![indexPath.row]["Poster"] as? String
if imageURL != "N/A" {
if let cover : UIImage = searchResults![indexPath.row]["Image"] as? UIImage {
cell.imageView?.image = cover
} else {
//Use default cover while the correct image downloads
//cell.imageView?.image = DEFAULT_COVER
downloadCover(imageURL!, tableViewRow: indexPath.row)
}
} else {
//Use default cover
//cell.imageView?.image = DEFAULT_COVER
}
return cell
}
不要使用可选类型。
声明一个非可选的空数组
var searchResults : [[String : Any]]()
然后,numberOfRowsInSection
可以
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return searchResults.count
}
要清除表视图,请写入
searchResults.removeAll()
tableView.reloadData()
没有拆封,没有检查
nil
,没有问题。这篇文章是很久以前的事了,但是对于任何感兴趣的人来说,下面是我在我的应用程序中使用的内容
如果确实要使用deleteRows
功能(例如用于动画目的),则可以执行以下操作:
let count = self.tableView(self.tableView, numberOfRowsInSection: 0)
// insert action that deletes all your data from the model here
// e.g. self.arrayOfRows = []
self.tableView.deleteRows(at: (0..<count).map({ (i) in IndexPath(row: i, section: 0)}), with: .automatic)
let count=self.tableView(self.tableView,numberOfRowsInSection:0)
//在此处插入从模型中删除所有数据的操作
//例如,self.arrayOfRows=[]
self.tableView.deleteRows(位于:(0..尝试以下操作:
tableView.getItems().clear();
我尝试了重置表视图的方法,它对我有效。表被清空。在你的问题中,你写道你将数据源设置为零。你还在其他地方这样做吗?不,我只在searchBarSearchButtonClicked(…)。当我说我将其设置为nil时,我的意思是我将tempDict创建为nil字典并设置searchResults=tempDict。我确实尝试只执行searchResults=nil,但它给了我一个错误。当尝试设置searchResults=nil
时,您会遇到什么样的错误?它不应该导致错误。它一定是打字错误。它现在可以工作了,但是我的TableView仍未清除。我将更新代码以显示更改。我发现了问题,这是我的错。我忘记清除从dbSearcher.startSearch(…)回调中传输的结果。我已按照您的建议更改了变量,但谢谢。唯一的区别是它必须声明为var searchResults:[[字符串:AnyObject]=[]