5

I have implemented a leading swipe action ('Delete') on my tableView which for a reason I can't figure out is also appearing as a trailing swipe action. See code below:

func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) ->
    UISwipeActionsConfiguration? {
    let delete1 = deleteAction(at: indexPath)
    return UISwipeActionsConfiguration(actions: [delete1])
}

func deleteAction(at indexPath: IndexPath) -> UIContextualAction {
    let action = UIContextualAction(style: .destructive, title: "Delete") { (action, view, completion) in
        self.delete(at: indexPath)
    }
    return action
}

I used to have a trailing swipe action, but I deleted this function out completely. When I change 'leadingSwipeActionsConfigurationForRowAt' to 'trailingSwipeActions...' then only the trailing swipe action appears. Be grateful if anyone could tell me what I've missed. Thanks. trailing swipe

leading swipe

Zenman C
  • 1,653
  • 13
  • 21

2 Answers2

5

Use this code to prevent trailingSwipeAction()

    func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle
    {
        return .none
    }
  • or
    func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
        return UISwipeActionsConfiguration(actions: [])
    }
steveSarsawa
  • 1,559
  • 2
  • 14
  • 31
1

Because that is the default behaviour, when swipes are enabled. You can do something like this to disable swipes on the trailing side, if you want to implement the destructive delete action on the left only.

func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
   return UISwipeActionsConfiguration(actions: [])
}

By passing an empty set of actions, the trailing swipe will disappear due to having 0 set of possible actions.

ChillY
  • 106
  • 6
  • Thanks for the quick response ChillY. The code seems to work. - I'm not sure why there has been the down vote? - funny though that the default for a 'leading' swipe action also enables it for a trailing swipe action, especially as there is also a trailing swipe action method... – Zenman C Jul 30 '19 at 06:55
  • I guess others wanted their answers on top, but on the topic - its the way I do it as well and having the delegate method with an empty array is clean to read as well, then it's good for anyone reading the code to understand, that there are no actions on trailing swipe :) – ChillY Jul 31 '19 at 19:30
  • @ChillY Thank you! In my app, my table has a list of events, but only the creator of an event can delete it. So, my `trailingSwipeActionsConfigurationsForRowAt` has a multi-layered conditional (ie, `if !userIsLoggedIn { showLoginAlert } else { if userIsCreator { delete } else { return ... (actions: [])`). I had been returning `nil` instead of what you suggested. THANK YOU! THANK YOU! – Zonker.in.Geneva Aug 12 '19 at 15:02
  • No, I don't think you understand how happy I am to have this solution! ;) I'd been scouring the internet and SO for days trying to figure this out. – Zonker.in.Geneva Aug 13 '19 at 07:24