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

2

20

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.

Hanky answered 25/9, 2017 at 21:49 Comment(2)
Is there a reason that you are using swipe actions for this case rather than edit actions? Edit actions are available since iOS 8.0+ rather than restricted to iOS 11.0+ so would provide more flexibility for devices which have not yet been upgraded to the recent iOS version. I mean this as a clarifying point, not an answer to your question.Thankful
I actually was using edit actions. I had implemented canEditRowAt and commit editing Style. But for some reason (even before implementing trailingSwipeActionsConfigurationForRowAt), the full swipe was enabled. I'm updating my question to include thisHanky
R
42

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.

Rusticate answered 26/9, 2017 at 2:39 Comment(4)
I had some logic in the commit editingStyle function before. Does this mean I transfer that logic into the code block of the UIContextualAction? This seems to work, but I'm wondering if that is the best practice.Hanky
You have to transfer this logic here.Rusticate
i am used "editActionsForRowAt" method to add the left swipe in tableview cell please how to all the stop the full swipe inside of this method. because i am used supper the device for below the iOS 10.0 also.? –Tetraploid
@ViniApp what line of code "completionHandler(true)" do in this situation?Emarie
S
3

I was able to disable swipe actions for particular cells by following this answer: https://mcmap.net/q/662414/-delete-button-is-automatically-implemented-on-swipe-despite-not-implementing-trailing-swipe-action-in-tableview

Instead of returning nil you can return:

return UISwipeActionsConfiguration.init()
Steiner answered 9/8, 2019 at 15:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.