Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

One way to avoid UIAction being triggered multiple times when a UIButton in a CollectionViewCell is tapped is by disabling the button until the action is completed. You can use the addTarget method in the cellForRowAtindexPath method to add the target for the button. In the selector method, you can disable the button and then perform the action. Once the action is completed, you can enable the button again. Here's an example:

First, declare a boolean variable:

var buttonIsTapped = false

Next, add the target to the button in cellForRowAtindexPath method:

cell.button.addTarget(self, action: #selector(buttonTapped(_:)), for: .touchUpInside)

In the selector method, disable the button and perform the action:

@objc func buttonTapped(_ sender: UIButton) {
    if !buttonIsTapped {
        buttonIsTapped = true
        sender.isEnabled = false
        //perform action
        sender.isEnabled = true
        buttonIsTapped = false
    }
}

This will ensure that the button is disabled until the action is completed, and will prevent the UIAction from being triggered multiple times.