0

I have a custom UICollectionViewCell with a button called collectionViewButton inside the cells.

  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CustomCollectionViewCell


    cell.collectionViewButton.tag = indexPath.item
    cell.collectionViewButton.addTarget(self, action:#selector(CollectionViewController.collectionButtonAction(_:)), for: .touchUpInside)

    ...

}

which calls the function:

func collectionButtonAction(_ sender: UIButton){
    ...
}

When the button is pressed the error:

'[ProjectName.CustomCollectionViewCell collectionButtonAction:]: unrecognized selector sent to instance' appears.

CustomCollectionViewCell code:

class CustomCollectionViewCell: UICollectionViewCell {

@IBOutlet weak var collectionViewButton: UIButton!

}
Artem Novichkov
  • 2,356
  • 2
  • 24
  • 34
Ryan Hampton
  • 319
  • 1
  • 4
  • 21
  • A pattern I often use (which will let you easily associate the button press with the cell and associated data), is to declare a closure inside the cell, such as `var onButtonPress: ((Void) -> Void)?`. Then, in the cell class, I'd add the `collectionButtonAction` function (and add it as the button's target action) and call `onButtonPress` within it. Finally, in `cellForItemAt`, I'd simply assign a closure to `cell.onButtonPress` and do whatever I want to do when the button is pressed. – paulvs Dec 27 '16 at 15:27
  • What is the complete and exact error message? You left out all of the import parts of the message. – rmaddy Dec 27 '16 at 15:29
  • I'll edit the post now – Ryan Hampton Dec 27 '16 at 15:35
  • What is the name of the class which implements the collection view data source? it probable that the name is not **CollectionViewController** as you have used in `#selector(CollectionViewController.collectionButtonAction(_:))`. Try to use just `#selector(collectionButtonAction:)` . [See this answer](http://stackoverflow.com/a/39205650/2604204) – Luiz Henrique Guimaraes Dec 27 '16 at 15:41
  • Changing the code to that throws the same error – Ryan Hampton Dec 27 '16 at 15:44

2 Answers2

0

Add one target for your collectionViewButton in cell initialisation method and call closure in property, like:

class CustomCollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var collectionViewButton: UIButton!
    var actionHandler: (() -> Void)?

    init() {
        super.init(frame: .zero)
        collectionViewButton.addTarget(self, action:#selector(buttonAction), for: .touchUpInside)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func buttonAction() {
        actionHandler?()
    }
}

Set needed handler in cellForItemAt method of collection view delegate.

Artem Novichkov
  • 2,356
  • 2
  • 24
  • 34
0

Change this below line

cell.collectionViewButton.addTarget(self, action:#selector(CollectionViewController.collectionButtonAction(_:)), for: .touchUpInside)

to

cell.collectionViewButton.addTarget(self, action:#selector(self.collectionButtonAction(_:)), for: .touchUpInside)

try this once.

Mahesh Agrawal
  • 3,348
  • 20
  • 34