How to properly "Delete a row from a tableView" using trailingSwipeActionsConfiguration

I have been stuck on this for some time now, I built a tableView App with the ability to delete a row. I am using with CoreData and iCloud. When I use "editActionsForRowAt" it deletes from the view and does not re-appear when I close and run the App again, the problem is as you know it is deprecated in iOS 13.

1.) This works great but is deprecated in iOS 13

override func tableView(_ tableView: UITableView,   editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
//Social Sharing Button 
let shareAction = UITableViewRowAction(style:  UITableViewRowAction.Style.default, title: "share", handler: { (action, indexPath) -> Void in
_ = "Just checking in at " + self.inspections[indexPath.row].stateID!
  })
// Delete Button
  let deleteAction = UITableViewRowAction(style: UITableViewRowAction.Style.default, title: "Delete",handler: { (action, indexPath) -> Void in

      if let appDelegate = (UIApplication.shared.delegate as? AppDelegate) {
          let context = appDelegate.persistentContainer.viewContext
          let inspectionsToDelete = self.fetchResultController.object(at: indexPath)
          context.delete(inspectionsToDelete)
          appDelegate.saveContext()
      }
  })
 deleteAction.backgroundColor = UIColor(red: 237.0/255.0, green: 66.0/255.0, blue: 106.0/255.0, alpha: 1.0)
  shareAction.backgroundColor = UIColor(red: 63.0/255.0, green: 212.0/255.0, blue: 78.0/255.0, alpha: 1.0)
  return [deleteAction, shareAction]
 }

2.) So I ran with "trailingSwipeActionsConfigurationForRowAt" This should work but it will not swipe to delete the row.

func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationsForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let deleteAction = UIContextualAction(style: .normal, title: "Delete", handler: {(action, view, success) in
if let appDelegate = (UIApplication.shared.delegate as? AppDelegate) {
let context = appDelegate.persistentContainer.viewContext
let inspectionsToDelete = self.fetchResultController.object(at: indexPath)
context.delete(inspectionsToDelete)
appDelegate.saveContext()
}
})
return UISwipeActionsConfiguration(actions: [deleteAction])
}

3.) This deletes without 1.) or 2.) but repopulates the row when I reopen the App

    override func tableView(_ tableView: UITableView, commit   editingStyle: UITableViewCell.EditingStyle, forRowAt     indexPath: IndexPath) {
 if editingStyle == .delete {
    //  delete the row from the data source
    inspections.remove(at: indexPath.row)
    }
    tableView.deleteRows(at: [indexPath], with: .fade)
    }


Answered by drewgost in 708583022
Found my solution!

override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationsForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let deleteAction = UIContextualAction(style: .destructive, title: "Delete") { (action, sourceView, completionHandler) in
if let appDelegate = (UIApplication.shared.delegate as? AppDelegate) {
let context = appDelegate.persistentContainer.viewContext
let inspectionsToDelete = self.fetchResultController.object(at: indexPath)
context.delete(inspectionsToDelete)
appDelegate.saveContext()
}
completionHandler(true)
}

Thanks for any help getting me in the right direction.

Drew

Accepted Answer
Found my solution!

override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationsForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let deleteAction = UIContextualAction(style: .destructive, title: "Delete") { (action, sourceView, completionHandler) in
if let appDelegate = (UIApplication.shared.delegate as? AppDelegate) {
let context = appDelegate.persistentContainer.viewContext
let inspectionsToDelete = self.fetchResultController.object(at: indexPath)
context.delete(inspectionsToDelete)
appDelegate.saveContext()
}
completionHandler(true)
}
How to properly "Delete a row from a tableView" using trailingSwipeActionsConfiguration
 
 
Q