I implemented editActionsForRowAtIndexPath and commitEditingStyle but no edit actions appear on the tableViewCell when swiping the cell

Abdelrahman picture Abdelrahman · Jun 23, 2015 · Viewed 13k times · Source

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

Answer

gyan picture gyan · Jun 23, 2015

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).