Allow UITableView to reorder, but not delete in edit mode, and also delete swipe to delete anyway

I have a UITableView (iOS 9) I performed two actions using a swipe (one of them is deleted) I have an “Edit” button to enable editing mode (to change the order of the lines)

For this, I implemented

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

Everything works fine, but I want the edit mode to work only for reordering. I do not want the red minus sign to appear, but I want everything to be done.

Is it possible? It seems that disabling deletion in edit mode disables swipe to delete gestures.

+4
source share
1 answer

, , , - editingStyleForRowAtIndexPath. .Delete , .

( ), , .

+2

Source: https://habr.com/ru/post/1625659/


All Articles