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

6

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

Kary answered 23/6, 2015 at 13:23 Comment(2)
do you impliemtn canEditRowAtIndexPath to return YESNecktie
yes I did and it works in another viewController without implementing canEditRowAtIndexPathKary
V
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).

Valorize answered 23/6, 2015 at 14:14 Comment(4)
I implemented editActionsForRowAtIndexPath but the accessory view is not shownKary
if I don't implement func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) the swiping is not workingKary
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.Oliva
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 buttonCatcher
C
3

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

Cluck answered 23/6, 2015 at 13:29 Comment(4)
I have already implement canEditRowAtIndexPath but still not workingKary
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 wellKary
Is the other controller a subclass of UIViewController or a subclass of UITableViewController or some other class where you are inheriting the functionality?Cluck
The other controller is a subclass of UIViewController and is the delegate of the tableviewKary
E
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]
    }
Emotionalism answered 11/9, 2015 at 10:0 Comment(0)
M
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.

Micheal answered 4/6, 2017 at 4:12 Comment(0)
W
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
}
Waterside answered 1/8, 2017 at 8:59 Comment(0)
K
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?

Kary answered 25/6, 2015 at 18:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.