Peter Johnson
Peter Johnson

Reputation: 3786

How can I get iOS to show a delete button on a tableView, but not allow a full swipe from the right to delete

A full swipe from the right to delete from a tableView is too dangerous for my app, as my users are reporting accidentally losing data.

I COULD add an "are you sure?" prompt, but instead I'd like to allow the delete button to appear, but disable the full swipe behaviour.

How can I do that?

Upvotes: 0

Views: 100

Answers (1)

Peter Johnson
Peter Johnson

Reputation: 3786

Do this by replacing the usual delete button behaviour with your own copy, with performsFirstActionWithFullSwipe set to FALSE

    - (UISwipeActionsConfiguration *) tableView:(UITableView *)tableView
    trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath {
        //optional- returns previous behaviour when table is in edit mode
        if (tableView.editing ) {
            return nil;
        }

        UIContextualAction *deleteAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"Delete" 
handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
                 // call your existing delete code
                [self tableView:tableView commitEditingStyle:UITableViewCellEditingStyleDelete forRowAtIndexPath:indexPath ];
        }];

        UISwipeActionsConfiguration *config = [UISwipeActionsConfiguration configurationWithActions:@[deleteAction]];
        config.performsFirstActionWithFullSwipe=FALSE; // this is why we are replacing the delete button!
        return config;
    }

Upvotes: 1

Related Questions