Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 365
Megan Moreno Avatar asked Sep 25 '17 21:09

Megan Moreno


1 Answers

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.

like image 180
Vini App Avatar answered Sep 24 '22 10:09

Vini App