8
votes

I have a UITableView (iOS 9) I have implemented two actions with swipe (one is delete) I have an Edit button to enable edit mode (to reorder the rows)

For that, 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 is working fine, but I want the edit mode just working for reordering. I don't want the red minus sign to appear, but I want to keep the swipe actions.

Is this possible? It seems that disabling delete in edit mode does disable the swipe to delete gesture.

1
Update your question with your relevant table view methods so we can see what you have in place so far. Just post all of the methods related to editing, deleting, and moving.rmaddy
You need to show your actual implementation of those methods.rmaddy
I don't think posting the actual implementation will help. It will just make the question longer. When writing standard code for this methods, it gives the result I have (standard) of having the delete and other action in swipe to delete, and having it again in edit mode with the minus style. If this is possible, that's something I should add to that, not something badly implemented, IMHOFredericP
Posting your code is very helpful. It allows us to see what you actually have and makes it much simpler to tell you what needs to be changed.rmaddy

1 Answers

4
votes

I believe the only thing you need to change in your code is the editingStyleForRowAtIndexPath function. You only return .Delete if the table view is not in editing mode.

This way swipe-to-delete still works (not in editing mode), and when you do switch to editing mode, the row can't be deleted.