Search code examples
iosuitableviewswipeios11swift4

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.


Solution

  • 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.