Megan Moreno
Megan Moreno

Reputation: 213

How do I disable the full swipe on a tableview cell in iOS11

UITableViewDelegate.h

// Swipe actions
// These methods supersede -editActionsForRowAtIndexPath: if implemented
// return nil to get the default swipe actions
- (nullable UISwipeActionsConfiguration *)tableView:(UITableView *)tableView leadingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(tvos);
- (nullable UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(tvos);

However, I am returning nil in my trailingActions method and I still can do a full swipe to delete in my tableview. How can I prevent the full swipe? (I want the user to have to swipe then press the "Delete" button.

@available(iOS 11.0, *)
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    return nil
}

EDIT: I had implemented canEditRowAt and commit Editing style before the iOS 11/XCode 9/Swift 4 update. The full swipe was enabled even BEFORE I implemented the trailingSwipeActionsConfigurationForRowAt.

Upvotes: 20

Views: 9313

Answers (2)

virtas
virtas

Reputation: 63

I was able to disable swipe actions for particular cells by following this answer: https://stackoverflow.com/a/50597672/1072262

Instead of returning nil you can return:

return UISwipeActionsConfiguration.init()

Upvotes: 3

Vini App
Vini App

Reputation: 7485

Implement like below :

func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    let delete = UIContextualAction(style: .destructive, title: "Delete") { (action, sourceView, completionHandler) in
        print("index path of delete: \(indexPath)")
        completionHandler(true)
    }
    let swipeAction = UISwipeActionsConfiguration(actions: [delete])
    swipeAction.performsFirstActionWithFullSwipe = false // This is the line which disables full swipe
    return swipeAction
}

This is the line which disables full swipe

swipeAction.performsFirstActionWithFullSwipe = false 

And remove the other functions if you implement any like editingStyle and editActionsForRowAt.

Upvotes: 42

Related Questions