在swift中手动调用editActionsForRowAtIndexPath

时间:2016-08-17 06:14:13

标签: swift uitableview swipe didselectrowatindexpath

我想点击该单元格,然后调用编辑操作。但是,我不确定如何实现手动调用滑动。我认为这个答案有一个解决方案,但我不太了解客观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]
}

2 个答案:

答案 0 :(得分:4)

这是方法:

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]
}

你的结果将是:

enter image description here

希望这会有所帮助。

答案 1 :(得分:0)

您可以使用此委托方法进行滑动删除。

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

        return UITableViewCellEditingStyle.Delete

  }