Why isn't reloadData() not working after deleting cells in collection view?

250 views Asked by At

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")
    
}
1

There are 1 answers

0
Rob On

The reloadData must be after the deleteAllRecords. But if you have swapped them and are still are having the problem, then either:

  • your delete process is running asynchronously (as is the case with many database APIs) and you’re not reloading when the asynchronous process is done; or
  • you are not updating your model (probably an array) once the records are deleted.