вызвать editActionsForRowAtIndexPath вручную в быстром режиме

Я хочу щелкнуть ячейку и вызвать действие редактирования. Но я не уверен, как реализовать вызов смахивания вручную. Я думаю, что в этом ответе есть решение, но я не совсем понимаю цель 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]
}

person Tony    schedule 17.08.2016    source источник


Ответы (2)


Вот способ для этого:

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

И ваш результат будет:

введите здесь описание изображения

Надеюсь, это поможет.

person Dharmesh Kheni    schedule 17.08.2016
comment
Есть ли способ показать кнопки удаления и редактирования напрямую или поместить кнопки удаления и редактирования слева? - person Tony; 17.08.2016

Вы можете использовать этот метод делегата для удаления смахивания.

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

        return UITableViewCellEditingStyle.Delete

  }
person Subin K Kuriakose    schedule 17.08.2016
comment
Я хочу, чтобы ячейка была проведена и отображены кнопки ИЗМЕНИТЬ и УДАЛИТЬ, щелкнув ячейку - person Tony; 17.08.2016