Search code examples
ios3dtouch

How to reload UITableView after deleting an item using UIPreviewAction


I am using UIPreviewAction to delete an item from a Core Data data source. Peek action is initiated from a cell on a UITableView that uses the same data source.

My problem is that as the code for UIPreviewAction is part of the peeked view controller's code, how should I signal back to the original UITableViewController that it should reload its data source?


Solution

  • This can be done in multiple ways you can use notificationcentre like this :

    Fire the notification using this code where you are deleting

    NSNotificationCenter.defaultCenter().postNotification(NSNotification(name: "reloadTable", object: nil))
    

    The receiver action will be in your controller where the table view exists.Write this line in viewdidload and don't forget to remove the observer in deinit function

    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.reloadTable), name: "reloadTable", object: nil)
    

    Finally the function

     func reloadTable() {
              tableView.reloadData()
           }