允许UITableView重新排序,但不允许在编辑模式下删除,并且无论如何都允许滑动以删除

Fre*_*icP 5 uitableview ios

我有一个UITableView(iOS 9),我通过滑动实现了两个操作(一个是删除操作),我有一个“编辑”按钮以启用编辑模式(对行进行重新排序)

为此,我实施了

    override func setEditing(editing: Bool, animated: Bool) {

    super.setEditing(editing, animated:animated)

    if (!isInSwipeDeleteMode) {
        if (self.tableView.editing) {
            metAJourPersonnes()
            tableView.reloadData()
        }
        else {
            tableView.reloadData()
        }
    }
}

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

    if (indexPath.row < personnes.count) {
        return true
    } else {
        return false
    }
}

override func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {
    let pers = personnes[sourceIndexPath.row]
    personnes.removeAtIndex(sourceIndexPath.row)
    if (destinationIndexPath.row < personnes.count)
    {
        personnes.insert(pers, atIndex: destinationIndexPath.row)
    } else {
        personnes.append(pers)
    }
    tableView.reloadData()
}

override func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
    if (indexPath.row < personnes.count) {
        return .Delete
    } else {
        return .None
    }
}

override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
    let deleteClosure = { (action: UITableViewRowAction!, indexPath: NSIndexPath!) -> Void in
        print("Delete closure called")
        self.tableView(tableView, commitEditingStyle: .Delete, forRowAtIndexPath: indexPath)
    }

    let modifyClosure = { (action: UITableViewRowAction!, indexPath: NSIndexPath!) -> Void in
        print("More closure called")            
        self.performSegueWithIdentifier("modifPersonne", sender: indexPath)
    }

    let deleteAction = UITableViewRowAction(style: .Default, title: "Supprimer", handler: deleteClosure)
    let modifyAction = UITableViewRowAction(style: .Normal, title: "Modifier", handler: modifyClosure)   
    return [deleteAction, modifyAction]

}

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    switch editingStyle {

    case .Delete:
        // Delete in the coreData base         
        personnes.removeAtIndex(indexPath.row)
        tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)

    default:    break
    }
}
Run Code Online (Sandbox Code Playgroud)

一切工作正常,但我希望编辑模式仅适用于重新排序。我不希望红色减号出现,但我想保持滑动动作。

这可能吗?似乎在编辑模式下禁用删除确实会禁用滑动以删除手势。

rma*_*ddy 5

我相信您的代码中唯一需要更改的是函数editingStyleForRowAtIndexPath.Delete仅当表视图未处于编辑模式时才返回。

这样,滑动删除仍然有效(不在编辑模式下),并且当您切换到编辑模式时,无法删除该行。