如何通过滑动操作设置 UITableView 编辑模式?

How to set a UITableView Editing Mode from a swipe action?

我需要通过轻扫操作“移动”中的其中一个单元格来打开 tableView 编辑模式:

滑动动作代码:

override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    let move = UIContextualAction(style: .normal, title: "Переместить") { (action, view, completionHandler) in
        self.turnEditing()
        completionHandler(true)
    }
    move.backgroundColor = UIColor(named: "CalmBlueColor")
    
    let configuration = UISwipeActionsConfiguration(actions: [move])
    return configuration
}

turnEditing() 函数:

   func turnEditing() {
       if tableView.isEditing {
           tableView.isEditing = false
       } else {
           tableView.isEditing = true
       }
    }

TableView 代表:

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

override func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
    
}

override func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
    return .none
}

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

当我按下滑动操作时,它只是关闭,没有进入编辑模式... 这是 GIF

是否可以通过滑动操作或仅通过 barButtonItem + IBAction 进入编辑模式?

添加一些延迟

DispatchQueue.main.asyncAfter(deadline: .now() + 0.75) {
  self.turnEditing()
}

顺便说一句,你可以替换这个

if tableView.isEditing {
  tableView.isEditing = false
} else {
  tableView.isEditing = true
}

tableView.isEditing.toggle()