Propagate a custom event from a UICollectionViewCell

Viewed 1312

I have a custom UICollectionViewCell with a button inside it. When I tap the button, an event is fired inside that subclass. I want to then trigger an event on the UICollectionView itself, which I can handle in my view controller.

Pseudo-code:

class MyCell : UICollectionViewCell {
    @IBAction func myButton_touchUpInside(_ sender: UIButton) {
        // Do stuff, then propagate an event to the UICollectionView
        Event.fire("cellUpdated")
    }
}

class MyViewController : UIViewController {
    @IBAction func collectionView_cellUpdated(_ sender: UICollectionView) {
        // Update stuff in the view controller 
        // to reflect changes made in the collection view
    }
}

Ideally, the event I define would appear alongside the default action outlets in the Interface Builder, allowing me to then drag it into my view controller code to create the above collectionView_cellUpdated function, similar to how @IBInspectable works in exposing custom properties.

Is there any way to implement a pattern like this in native Swift 3? Or if not, any libraries that make it possible?

2 Answers
Related