Basically Im pressing the delete button to delete all the cells and when I call the reloadData() it doesn't clear the cells. The only way the cells get cleared is when I leave the collection view screen and come back to it. Why does that happen?
@IBAction func deleteCell(_ sender: Any) {
//alert controller pops up
let alertController = UIAlertController(title: "Do you want to delete all images?", message: "", preferredStyle: .alert)
let action1 = UIAlertAction(title: "Delete", style: .default) { (action) in
self.collectionView.reloadData()
DataBaseHelper.instance.deleteAllRecords()
print("delete all")
}
//when pressed cancel alert sheet is dismissed and nothing happens
let action2 = UIAlertAction(title: "Cancel", style: .cancel) { (action) in
print("Cancel is pressed......remove alert sheet")
}
alertController.addAction(action1)
alertController.addAction(action2)
self.present(alertController, animated: true, completion: nil)
print("delete all cells")
}
The
reloadDatamust be after thedeleteAllRecords. But if you have swapped them and are still are having the problem, then either: