I'd like to mimic the swipe to delete function of a UITableViewCell just like the mail app in iOS 8. I'm not referring to swipe to reveal a delete button. I'm referring to when you swipe, it discoloses 3 actions, but if you keep swiping to the left, the email is deleted.
In iOS 8, UITableView has a new method where you can provide the data to display any number of buttons:
#ifdef __IPHONE_8_0
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewRowAction *viewStackRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Stack" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
SM_LOG_DEBUG(@"View Stack Action");
}];
viewStackRowAction.backgroundColor = [UIColor radiusBlueColor];
UITableViewRowAction *viewUserRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"User" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
SM_LOG_DEBUG(@"View User Action");
}];
viewUserRowAction.backgroundColor = [UIColor radiusLightBlueColor];
UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Delete" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
SM_LOG_DEBUG(@"Delete");
}];
deleteRowAction.backgroundColor = [UIColor redColor];
return @[deleteRowAction, viewUserRowAction, viewStackRowAction];
}
#endif
I don't see any API to detect if you keep swiping though. I've grepped for 8_0 in UITableView.h and the above method seems to be the only new one.
I suppose one could monitor the scroll view offset, or add/hijack a UIPanGestureRecognizer. I just wanted to make sure to use the default way, if there is one (and get the animation for "free")
With Swift 4.2 and iOS 12, according to your needs, you can choose one of the 3 following ways in order to create a trailing swipe action that will delete the selected
UITableViewCell
.#1. Using
UITableViewDataSource
'stableView(_:commit:forRowAt:)
When you use
tableView(_:commit:forRowAt:)
with aneditingStyle
of valueUITableViewCell.EditingStyle.delete
, full swipe to delete is automatically supported by the system.#2. Using
UITableViewDelegate
'stableView(_:editActionsForRowAt:)
andUITableViewRowAction
In order to support full swipe to delete with
UITableViewRowAction
, you have to initialize it with astyle
that has a value ofUITableViewRowAction.Style.destructive
.#3. Using
UITableViewDelegate
'stableView(_:trailingSwipeActionsConfigurationForRowAt:)
andUISwipeActionsConfiguration
(requires iOS 11)UISwipeActionsConfiguration
has a property calledperformsFirstActionWithFullSwipe
.performsFirstActionWithFullSwipe
has the following declaration:The following
UITableViewController
implementation show how to useUISwipeActionsConfiguration
in order to manage full swipe to delete actions.add ui gustere recognizer to each cell, check the amount of "swipness", if its above specific threshold, do the deletion.
somthing like:
You can use MGSwipeTableCell. They have implement this feature to fire callback swipeTableCell:tappedButtonAtIndex:direction:fromExpansion: with tappedButtonAtIndex equal to 0 (so it gets executed what you implemented on first added button).
Your table view's data source has to implement
otherwise the built-in iOS 8 swiping functionality will not work.
This seems counterintuitive since a
UITableViewRowAction
accepts a block. But it's the only way I've been able to get it to work.