如何禁用UITableViewRowAction(不单击编辑按钮)?

时间:2017-03-31 09:42:50

标签: uitableview swift3 tableviewcell

通常在调用此函数时,允许通过向左滑动删除表格视图单元格:

    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

    if editingStyle == UITableViewCellEditingStyle.delete {
        dataSource.remove(at: indexPath.row)

        tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.fade)
    }
}

但是我创建了一个这样的编辑按钮:

self.navigationItem.leftBarButtonItem = self.editButtonItem

并且我想仅在单击编辑按钮时启用删除单元格而不是仅通过滑动。这可能吗?

2 个答案:

答案 0 :(得分:2)

使用默认值Boolean创建一个false实例属性,并在true的操作后将其设置为BarButttonItem,然后使用canEditRowAt中的布尔属性允许/禁止行编辑的方法。

var allowEdit = false

override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    return allowEdit
}

注意:删除完成后,别忘了再次设置false

答案 1 :(得分:0)

我这样解决了:

    var editMode = false

...

    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {

    if editMode == true {
        return true
    } else {
        return false
    }
}

    override func setEditing(_ editing: Bool, animated: Bool) {
    super.setEditing(editing, animated: animated)

    if(editing) {
        tableView.reloadData()
        editMode = true
    }
    else if(!editing) {
        tableView.reloadData()
        editMode = false
    }
}