Mych Mych - 6 months ago 21
Swift Question

Customised EditAction Buttons don't trigger events

I have a tableviewcontroller with an Edit button in the navbar This has been overridden to show Options and Done instead of the Edit and Done..

In my viewDidLoad function I have...

// Display an Edit button in the navigation bar for this view controller.
self.navigationItem.leftBarButtonItem = self.editButtonItem()
// change initial text from Edit to Options
self.navigationItem.leftBarButtonItem!.title = "Options"


I also have the following...

override func setEditing (editing:Bool, animated:Bool)
{
//set different text for Edit and Done
super.setEditing(editing,animated:animated)
if(self.editing)
{
self.editButtonItem().title = "Done"
}else
{
self.editButtonItem().title = "Options"
}
}


For completeness this is my code for the edit action.

// Override to support editing the table view.
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {

switch editingStyle {

case .Delete:
let context = fetchedResultsController.managedObjectContext
if indexPath.row == 0 {
//this is the top (first row)
// Deleting without warning
let indexPathToDelete = NSIndexPath(forRow: 0, inSection: 0)
let objectToDelete = fetchedResultsController.objectAtIndexPath(indexPathToDelete) as! NSManagedObject
context.deleteObject(objectToDelete)

do {
try context.save()
} catch {
print(error)
}

} else {
//we are deleted a row that is not the top row
// we need to give a warning and if acknowledged then delele all rows from the selected row and all rows above it

let alertController = UIAlertController(title: nil, message: "Are you sure? This will remove this and all logs above it.", preferredStyle: .Alert)
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in

}
alertController.addAction(cancelAction)
let deleteAction = UIAlertAction(title: "Delete", style: .Default) { (action) in

for deleteindex in 0 ... indexPath.row {
let deleteIndexPath = NSIndexPath(forRow: deleteindex, inSection: 0)
let objectToDelete = self.fetchedResultsController.objectAtIndexPath(deleteIndexPath) as! NSManagedObject
context.deleteObject(objectToDelete)
}

do {
try context.save()

} catch {
print(error)
}
}
alertController.addAction(deleteAction)

// Dispatch on the main thread
dispatch_async(dispatch_get_main_queue()) {
self.presentViewController(alertController, animated: true, completion:nil)
}

}
break;

default :
return
}

}


This works fine...

Now I've now added custom edit actions... the usual Delete + Export by adding this code...

//Override edit actions
override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {

let delete = UITableViewRowAction(style: .Destructive, title: "Delete") { (action, indexPath) in
// delete item at indexPath
}

let export = UITableViewRowAction(style: .Normal, title: "Export") { (action, indexPath) in
// export item at indexPath
}

export.backgroundColor = UIColor.blueColor()

return [delete, export]
}


And now when in Edit mode each cell now has two options Export and Delete but neither do anything when pressed.

I have a breakpoint at the line case .Delete but it is not being hit.

Answer

You would have to place your deletion code into its Action:

// Override edit actions
override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {

    let delete = UITableViewRowAction(style: .Destructive, title: "Delete") { (action, indexPath) in
        // You code for Deletion here... 

    }
Comments