Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 752
Abdelrahman Avatar asked Jun 23 '15 13:06

Abdelrahman


4 Answers

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

like image 196
gyan Avatar answered Jan 02 '23 15:01

gyan


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

like image 40
Matt Rees Avatar answered Jan 02 '23 15:01

Matt Rees


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]
    }
like image 40
Rajesh Loganathan Avatar answered Jan 02 '23 15:01

Rajesh Loganathan


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.

like image 37
Achintya Ashok Avatar answered Jan 02 '23 16:01

Achintya Ashok