Tony
Tony

Reputation: 25

call editActionsForRowAtIndexPath manually in swift

I want to click the cell, and call the edit action. But, I am not sure how to implement calling the swipe manually. I think there is a solution in this answer, but I do not quite understand objective C. https://stackoverflow.com/a/29839673/5737856

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    // swipe
    //tableView(tableView, editActionsForRowAtIndexPath: indexPath) or other functions???
}

func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    return true
}

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
    let editAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "  Edit   " , handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in
        print("EDIT")
    })
    editAction.backgroundColor = UIColor.blueColor()

    let deleteAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Delete" , handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in
        print("DELETE")
    })
    return [deleteAction, editAction]
}

Upvotes: 2

Views: 2136

Answers (2)

Dharmesh Kheni
Dharmesh Kheni

Reputation: 71862

Here is the way for that:

var selectionIndexPath: NSIndexPath?

func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {

    if selectionIndexPath != nil {

        if selectionIndexPath!.row == indexPath.row {

            return true
        } else {

            return false
        }

    } else {

        return true
    }
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    selectionIndexPath = indexPath
    tableV.setEditing(true, animated: true)
}

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
    let editAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "  Edit   " , handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in
        print("EDIT")
    })
    editAction.backgroundColor = UIColor.blueColor()

    let deleteAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Delete" , handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in
        print("DELETE")
    })

    let cancleAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Cancle" , handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in

        self.tableV.setEditing(false, animated: true)
    })
    cancleAction.backgroundColor = UIColor.lightGrayColor()
    return [cancleAction, deleteAction, editAction]
}

And your result will be:

enter image description here

Hope this will help.

Upvotes: 4

Subin K Kuriakose
Subin K Kuriakose

Reputation: 849

You can use this delegate method to get swipe delete.

    func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {

        return UITableViewCellEditingStyle.Delete

  }

Upvotes: 0

Related Questions