Swift - Clear TableView

I have a search that loads a JSON file and displays the results in a TableView. If the user searches for something and receives a list of results, then he searches for something else that returns 0 results. The first result set is still in the TableView. I want to clear the TableView every time a new search starts to prevent this. I tried setting the data source to nil and reloading the TableView, but it does not work. Here is what I have:

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 } 
+5
source share
2 answers

Do not use extra type.
Declare an optional empty array.

 var searchResults : [[String : AnyObject]]() 

then numberOfRowsInSection might just be

 override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return searchResults.count } 

To clear the table view, write

 searchResults.removeAll() tableView.reloadData() 

No deployment, no verification for nil , no problem.

+10
source

This post was a long time ago, but for everyone who is interested, this is what I use in my application.

If you really want to use the deleteRows function (for example, for animation), you can do the following:

 let count = self.tableView(self.tableView, numberOfRowsInSection: 0) // insert action that deletes all your data from the model here // eg self.arrayOfRows = [] self.tableView.deleteRows(at: (0..<count).map({ (i) in IndexPath(row: i, section: 0)}), with: .automatic) 

It basically converts the values โ€‹โ€‹of the range 0..<count to [IndexPath] and instructs the table view to delete these paths using the deleteRows method.

+1
source

Source: https://habr.com/ru/post/1234636/


All Articles