8

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

Sujay
  • 2,510
  • 2
  • 27
  • 47
Abdelrahman
  • 997
  • 2
  • 10
  • 24

6 Answers6

9

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

COSMO BAKER
  • 337
  • 4
  • 16
gyan
  • 219
  • 1
  • 7
  • I implemented editActionsForRowAtIndexPath but the accessory view is not shown – Abdelrahman Jun 24 '15 at 08:19
  • if I don't implement func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) the swiping is not working – Abdelrahman Jun 24 '15 at 08:23
  • 1
    Verified on Xcode 7 GM candidate release that iOS 9 no longer requires the empty implementation of override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath). So if you baseline your deployment target to iOS9, you would not need the empty implementation. – rajagp Sep 11 '15 at 02:20
  • I'm using `func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: IndexPath) -> [AnyObject]? { let more = UITableViewRowAction(style: .normal, title: "more" , handler: { (action:UITableViewRowAction, indexPath: IndexPath) -> Void in }) return [more] }` but doesnt show me a `more` button, but a default `delete` button – S.Verma Mar 18 '17 at 20:55
3

You need to implement canEditRowAtIndexPath from the UITableview Delegate Methods and return true.

Matt Rees
  • 884
  • 8
  • 14
  • I have already implement canEditRowAtIndexPath but still not working – Abdelrahman Jun 23 '15 at 13:36
  • the problem is the editActions appear in another ViewController with and without implementing canEditRowAtIndexPath but in other ViewController they does not appear but the swipe is working well – Abdelrahman Jun 23 '15 at 13:38
  • Is the other controller a subclass of UIViewController or a subclass of UITableViewController or some other class where you are inheriting the functionality? – Matt Rees Jun 23 '15 at 13:40
  • The other controller is a subclass of UIViewController and is the delegate of the tableview – Abdelrahman Jun 23 '15 at 13:43
1

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]
    }
Rajesh Loganathan
  • 11,129
  • 4
  • 78
  • 90
1

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.

Achintya Ashok
  • 521
  • 4
  • 9
1

Sorry for waking up such an old thread, but I got it working on Swift 3 implementing:

func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
    return .delete
}
Jorge Ramos
  • 891
  • 10
  • 21
0

I just copied the ViewController where the row action buttons are shown and working well and replace the one where the row action buttons do not appear when sliding, and now the row actions buttons are shown and did the expected behavior.

But I do not understand the issue. does anyone can explain that?

Abdelrahman
  • 997
  • 2
  • 10
  • 24