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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With