I implemented editActionsForRowAtIndexPath
and commitEditingStyle
the swipe is working but no edit actions appear on the UITableViewCell
my implementation for editActionsForRowAtIndexPath
and commitEditingStyle
as follow:
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == UITableViewCellEditingStyle.Delete {
//I did some work here
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
}
}
func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {
let deleteAction = UITableViewRowAction(style: UITableViewRowActionStyle.Normal, title: "Delete" , handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in
//I did some work here
tableView.reloadData()
})
return [deleteAction]
}
Any help will be appreciated
I think you mixed two different kinds of editing here.
The first kind of editing is the old UITableViewCellEditingStyle.Delete
. And the new way is to provide your custom accessory view.
If you implement your custom accessory view, then the default delete buttons will not be shown, thus are not called. So your
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)
might not even be called, from my point of view.
Apple's documentation For editActionsForRowAtIndexPath
contains the following sentense : If you do not implement this method, the table view displays the standard accessory buttons when the user swipes the row.
I assumed that if you do implement this method, the standard accessory view will not be shown.
Edit: Code example (updated to Swift 3 11/17/16)
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
private func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: IndexPath) -> [AnyObject]? {
let deleteAction = UITableViewRowAction(style: UITableViewRowActionStyle.normal, title: "Delete" , handler: { (action:UITableViewRowAction, indexPath:IndexPath) -> Void in
})
return [deleteAction]
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
}
Edit 2: As rajagp points out, if you don't need an empty implementation if you are only targeting iOS9 (or later).
You need to implement canEditRowAtIndexPath from the UITableview Delegate Methods and return true.
Try to add some action inside delete button
func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]?
{
let delete = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Delete" , handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in
let alertView = UIAlertController(title: "Delete Action", message: "", preferredStyle: UIAlertControllerStyle.Alert)
alertView.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(alertView, animated: true, completion: nil)
})
delete.backgroundColor = UIColor.redColor()
let more = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "More" , handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in
let alertView = UIAlertController(title: "More Action", message: "", preferredStyle: UIAlertControllerStyle.Alert)
alertView.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(alertView, animated: true, completion: nil)
})
more.backgroundColor = UIColor.blueColor()
return [delete, more]
}
TLDR:
Necessary Functions to implement: • commitEditingStyle • canEditRowAt • editActionsForRowAt
Note: for editActionsForRowAt
you cannot return an empty array here -- it will screw up all the cells. If there is a particular type of row you don't want to allow edit actions for, specify this in canEditRowAt
to return false for that type of cell.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With