How to programmatically cancel a swipe to delete in iOS? - ios

I have a UITableView which has a swipe to delete action using:
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath)
This works perfectly fine, however there are some situation where the delete should not happen, in this situation when swiping to delete and pressing the delete button the button doesn't disappear but stays there as if nothing is happening.
It seems to me the delete button only collapses when the row is removed from the table (or its backing store).
Is there a way to tell the view to cancel the action?

Add the logic for determining if the row is eligible for editing to
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool (from UITableViewDataSource)
Of course this prevents all editing of the cell not just deleting, this may not be the way to go if you want to allow editing but not deleting.

Related

Enable Reorder Control in UITableView as default without being in edit mode

I am currently working on a solution where I need to have a UITableView by default in reorder mode without having a delete button but could enable the delete button when necessary. I am just curious if this is even achievable in iOS? If so what should be my approach for this?
In my opinion your table view should always be in edit mode.
To make the reorder controls appear you should implement UITableViewDataSource methods
tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool
and
tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath)
If you don't want the delete button to be displayed you can do that by implementing
tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle
and return .none by default or .delete when you want to have the button displayed.

Slide to delete with similar design to iOS 10+ Notifications slide to delete in UITableViewCell

How can I make a tableviewcell's slide to delete look like the slide to delete for iOS notifications (fade in and don't touch edge of the screen). I will only have the delete button so I don't need multiple buttons. I would like it to delete upon a full swipe just like the notifications.
Here's a photo of the wanted result with 2 buttons (I only want 1):
The current code I have written only sets the editing style to delete. I have tried using UIContextualAction but I believe I can only set the style, background color, and/or image.
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
print("delete")
}
}
This is what it looks like with my current code:
Try adding a UIView inside the UITableViewCell, with a red background, and a label inside the UIView with the text of delete. Good Luck!

iOS - disable right or left swipe for UITableViewCell

I have implemented iOS 11 trailingSwipeActionsConfigurationForRowAt and leadingSwipeActionsConfigurationForRowAt for my UITableViewCell. I'm using trailingSwipeActionsConfigurationForRowAt to disable the row, and leadingSwipeActionsConfigurationForRowAt to undo the disable of the row. I've tried to use canEditRowAt, but this disables editing of the entire row. Is there a way to disable only one of the swipe actions instead of disabling the entire row from editing?
I want to disable the undo swipe action if I already swiped to undo, but enable the cancel swipe action. I also want to disable the cancel swipe action if I already swiped to cancel, but enable the undo swipe action.
Try to return the empty array of actions in trailingSwipeActionsConfigurationForRowAt method to disable one sided action.
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let swipeAction = UISwipeActionsConfiguration(actions: [])
return swipeAction
}
Returning nil is sufficient to prevent the swipe action.
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
return nil
}

ios - UITableView delete button won't react to gesture

I have a custom table view cell that has a toggle button on the left side.
When the tableview's isEditing property is set to true, the delete editing option appears properly, but when it's tapped, nothing happens.
The stranger thing is, when I tap and hold, and then drag to one side away from the button, and THEN lift, the cell finally slides over. Which isn't how it's supposed to work at all.
Even when I remove connections to the toggle button from the storyboard and the subclass, it still behaves this way.
Why is it doing this?
First of all, correct me if I'm wrong, but I think these are two different problems.
1.Nothing happens when the "delete editing" button is tapped. I would check if the "delete editing" button properly connected to the class file with an IBAction.
2.The cell slide behavior is not what is desired. I would check to see if the gesture you're recognizing is a tap and not a left swipe.
Hope that helps.
you need to add following methods in your table view controller
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if (editingStyle == UITableViewCellEditingStyle.delete) {
//remove element from your array providing index.row
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
)
}
}

UITableView Swipe Not Called After editActionsForRowAt returns [ ]

I've been using editActionsForRowAt to create the built-in swipe left functionality to reveal quick-access buttons, like this:
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
print("Is this working?")
//Code to create UITableViewRowAction buttons
return [button1, button2]
}
func tableView (_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
//I read I need this, so I have it
return true
}
func tableView (_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
//I read I need this, so I have it
}
Now my whole table, including the swipe, works great. Until...
My editActionsForRowAt function has code where if the user swipes a cell that's expanded (by default they're 50, but users can tap to expand the height) then editActionsForRowAt returns [], an empty array. I do that because I don't want really tall cells showing stretched out buttons.
After that, even after closing, swiping a cell (any cell) doesn't even call editActionsForRowAt. I checked by adding a print-line right at the beginning of that function and it doesn't print no matter what I do after swiping an expanded cell and returning [].
Any ideas on how to fix this?
Instead of returning an empty array from editActionsForRowAt, you should return false from canEditRowAt when a given row isn't editable.

Resources