David Webb
David Webb

Reputation: 139

UITableView editActionsForRowAt not reacting properly to nil in Swift 5

I have had a UITableView working for several version of Swift, but with Swift 5, it started presenting 'delete' action on a left swipe.

One of the rows is 'swipe-able' (the 'Expanded' one) and others are not, so I return a nil in place of the RowAction.

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
    if listOfCurrentAwards[indexPath.row] != "Expanded" {
        print(">>> Skipping row \(indexPath.row)")
        return nil
    } else {
        let share = UITableViewRowAction(style: .normal, title: "Share") { action, index in
            self.prepareOneDogMessageShareSheet(row: indexPath.row)
        }
        share.backgroundColor = UIColor.blue
        return [share]
    }
}

I am also getting the same behavior with the older 'editActionsForRowAtIndexPath'.

Anyone else seeing the same thing? Did you find a work around?

For now I am just returning a dummy action that displays a dog related emoji ().

if listOfCurrentAwards[indexPath.row] != "Expanded" {
   //TBD - remove the following line if the nill action is supported/fixed.
   let dogEmoji = ["🐶","🐩","🦴","🐕","💩","🐾"]
   let share = UITableViewRowAction(style: .normal, title: dogEmoji.randomElement()) { action, index in
   print(">>> Skipping row \(indexPath.row)")
   }
   return [share] //nil
} else ...

Update 1 Even refactoring to use trailingSwipeActionsConfigurationForRowAt did not work, I'm getting the same result.

    func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
    {
        if listOfCurrentAwards[indexPath.row] != "Expanded" {
            print(">>> Swiping not enabled for row \(indexPath.row)")
            return nil
        } else {
            print(">>> 'Share' swipped on \(indexPath.row)")
            let shareAction = UIContextualAction(style: .normal, title: "Share") { (action, view, handler) in
                print(">>> 'Share' clicked on \(indexPath.row)")
                self.prepareOneDogMessageShareSheet(row: indexPath.row)
            }
            shareAction.backgroundColor = UIColor.blue
            let configuration = UISwipeActionsConfiguration(actions: [shareAction])
            configuration.performsFirstActionWithFullSwipe = true //false to not support full swipe
            return configuration
        }
    }

Upvotes: 3

Views: 2120

Answers (2)

David Webb
David Webb

Reputation: 139

Answer I had to add the canEditRowAt helper, allow me to move some of the logic from trailingSwipeActionsConfigurationForRowAt.

    func tableView(_ tableView: UITableViewbcgdfgdfg, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?vdfgh
    {
        print(">>> Trying to swipe row \(indexPath.row)")
        let shareAction = UIContextualAction(style: .normal, title: "Share") { (action, view, handler) in
            print(">>> 'Share' clicked on \(indexPath.row)")
            self.prepareOneDogMessageShareSheet(row: indexPath.row)
        }
        shareAction.backgroundColor = UIColor.blue
        let configuration = UISwipeActionsConfiguration(actions: [shareAction])
        return configuration
    }

    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return listOfCurrentAwards[indexPath.row] == "Expanded"
    }

Upvotes: 4

matt
matt

Reputation: 535138

editActionsForRowAt is outmoded if the goal is to control what happens when you swipe. So is UITableViewRowAction.

You should be using tableView(_:trailingSwipeActionsConfigurationForRowAt:).

Upvotes: 2

Related Questions