Andrew P.
Andrew P.

Reputation: 81

iOS - disable right or left swipe for UITableViewCell

I have implemented iOS 11 trailingSwipeActionsConfigurationForRowAt and leadingSwipeActionsConfigurationForRowAt for my UITableViewCell. I'm using trailingSwipeActionsConfigurationForRowAt to disable the row, and leadingSwipeActionsConfigurationForRowAt to undo the disable of the row. I've tried to use canEditRowAt, but this disables editing of the entire row. Is there a way to disable only one of the swipe actions instead of disabling the entire row from editing?

I want to disable the undo swipe action if I already swiped to undo, but enable the cancel swipe action. I also want to disable the cancel swipe action if I already swiped to cancel, but enable the undo swipe action.

Upvotes: 7

Views: 2153

Answers (2)

itwend
itwend

Reputation: 41

Returning nil is sufficient to prevent the swipe action.

func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {

   return nil
}

Upvotes: 0

pkc
pkc

Reputation: 8504

Try to return the empty array of actions in trailingSwipeActionsConfigurationForRowAt method to disable one sided action.

func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {

   let swipeAction = UISwipeActionsConfiguration(actions: [])

   return swipeAction
}

Upvotes: 13

Related Questions