I want to customise the delete indicator for a UITableView in editing mode.
Since I can't seem to find a way to customise the appearance style of the delete button shown in the first image, I've created my own animation with my own button.
I have deactivated the red standard button using:
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
return .none
}
Now my question: How do I realize the same behaviour as the standard button? If you press the red button, it invokes a swipe left on the cell showing the wanted delete button.
Help is very appreciated.
Related
In UITableView, when tapping and holding a cell and (without releasing this cell) switching to another screen in tab bar and returning to the initial one, the cell remains highlighted (not selected) and if tapping on another cell, the first one gets selected.
Upon tapping and holding the first cell, tableView(_ tableView: UITableView, didHighlightRowAt indexPath: IndexPath) is called for the first cell. So far it is how it should be.
Upon tapping on another cell (after returning to the screen), tableView(_ tableView: UITableView, didUnhighlightRowAt indexPath: IndexPath) is called for the first cell. And then all methods for selection for the first cell.
Is there a way to remove this behaviour? And upon returning to the screen have no highlighted cells.
cell.setHighlighted(false, animated: false) doesn't work. Yes, it removes highlighting, but selection methods are again called for the first cell.
Have you tried to implement shouldHighlightRowAt and return false?
From the docs for tableView(_:shouldHighlightRowAt:):
Asks the delegate if the specified row should be highlighted.
I'm trying to implement two cells, one that can be deleted by swiping, and one that can't be deleted, but has swipe actions the other way.
This is what it looks like now:
I don't want the middle cell to be indented when clicking Edit.
Without any custom code, all cells would show this red circle and get a delete-button, but I have added a few lines of code to prevent that.
This code will set all cells without a leading or trailing swipe action to not become editable, which explains the bottom cell in the gif. I have to include the leading swipe actions to this, because if the cell isn't editable, then I can't swipe at all.
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
let vm = sections[indexPath.section].viewModels[indexPath.row]
return vm.leadingSwipeActions != nil || vm.trailingSwipeActions != nil
}
The following code will prevent the middle cell from showing the red circle.
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
return sections[indexPath.section].viewModels[indexPath.row].trailingSwipeActions == nil ? .none : .delete
}
If I don't pass .none for the cell with leading actions, then it would show the red circle AND it would even show a default deletion-swipe. I don't want the ability to delete this row!
How can I prevent the middle cell from indenting, while still retaining the ability to swipe it?
This method seems to work! tableView(_ tableView: UITableView, shouldIndentWhileEditingRowAt indexPath: IndexPath) -> Bool
https://developer.apple.com/documentation/uikit/uitableviewdelegate/1614873-tableview?language=objc
I have two action on a table view cell like such:
After I touch them, the actions get executed but the cell goes back to its original state (closed) like such:
How can I prevent the cell from closing and keep show the actions after the touch?
Are you using trailingSwipeActionsConfigurationForRowAt to show the options, if yes then it is the default swipe behavior of UITableViewCell. On any action on the + or - the cell will hide those options, it does not retain it's trailingSwipeActions and goes back to the normal visible cell state.
func tableView(_ tableView: UITableView,
trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
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)
)
}
}
I have a UITableView, which comes prefitted with an Edit button (UITableView.editButtonItem()). When pressed, it toggles to "Done", and you get those red minus signs next to entries for when you want to delete them.
I want to attach custom code to the button so that when you click it, you don't get the red minus signs. When in edit mode, whenever you click on an entry, it brings up a UIActionSheet or a UIAlertController to allow you to make the changes.
So, I want to override the Edit button so that when clicked:
The red minus "Delete" icons don't show up
The cells become selectable.
I've tried creating a custom UIBarButtonItem, but that's a terrible walkaround. I think what I want is do-able, I'm just not sure how.
Use delegate for UIViewController and make custom code on the events of that button. If it is on every row you must attach tap action from the code during cell generation.
I myself would preffer to have custom class.
I've figured out which methods to call:
// The following two functions remove the red minus sign
override func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
return UITableViewCellEditingStyle.None
}
override func tableView(tableView: UITableView, shouldIndentWhileEditingRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return false
}
// The following property allows you to select cells while in editing mode
self.tableView.allowsSelectionDuringEditing = true