Shaideru

All things iOS & Swift

Adding delete option when swiping tableviewcell

You have to override the function commitEditingStyle,IMG_7069

[1]created appDel and context variables to access values from persistence store.
[2]inside the conditional statement, it removes a value from the array myTest.

Study the codes below.

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {

let appDel: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let context: NSManagedObjectContext = appDel.managedObjectContext!

if editingStyle == UITableViewCellEditingStyle.Delete {

if let tv = tableView as UITableView? {
context.deleteObject(myTest[indexPath.row] as NSManagedObject)
myTest.removeAtIndex(indexPath.row)
tv.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Fade)
tableView.reloadData()

}
}
var error:  NSError? = nil
if !context.save(&error) {
abort()
}
}

Leave A Comment

Your email address will not be published.