Zenman C
Zenman C

Reputation: 1723

Why is the leading swipe action also duplicated as a trailing action?

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

Upvotes: 5

Views: 946

Answers (2)

steveSarsawa
steveSarsawa

Reputation: 1679

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: [])
    }

Upvotes: 5

ChillY
ChillY

Reputation: 125

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.

Upvotes: 1

Related Questions