1

I have a collection view where each of the cells has a delete button. I added the following code to cellForItemAt indexPath function.

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellTwo", for: indexPath) as! CustomCellTwo

cell.deleteButton.layer.setValue(indexPath.row, forKey: "index")

cell.deleteButton.addTarget(self, action: #selector(deleteCell), for: .touchUpInside)

Initially it looked as if it was working great. However, I found out that the add target function does not get called at the first tap if I scroll back and forth and then tap the delete button. If I tap again, it works as expected. Only the first tap does not work.

I have been trying to find a reason and a solution for several hours... Please help provide any ideas and advice.

Jawad Ali
  • 13,556
  • 3
  • 32
  • 49
  • so the addTarget is working after you touch collectionView ... but not working until scroll – Jawad Ali Jun 22 '20 at 07:17
  • add target is working when I tap the button without scrolling. But it is strange that if I scroll back and forth and then tap a cell that went out of the screen and came back on the screen (because of the scrolling), then add target is not working in the first tap. The button visually reacts to my tap, but add target is not getting called. Add target is only working when I tap the cell again, so it works in my second tap (but only if I don't scroll again before my second tap). It looks like when the cell gets scrolled away, the collection view controller becomes confused with the buttons... – Gilbert Tyler Jun 22 '20 at 15:10
  • better design idea isto having action in cell class rather than in controller class – Jawad Ali Jun 22 '20 at 15:17

1 Answers1

0

Try to move buttons handling into CustomCellTwo implementation. Handle button event touchUpInside with @IBAction func. Now you can debug it with breakpoint set in this function's body. Also add closure type variable to your CustomCellTwo to pass deleteCell calls into it. So it could also be checked with breakpoint. Example:

class CustomCellTwo: UICollectionViewCell {
    var onDelete: (() -> Void)?
    @IBAction func onDeleteButtonTouch(_ sender: Any) {
        onDelete?()
    }
}


// in your UICollectionViewDataSource

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellTwo", for: indexPath) as! CustomCellTwo
    cell.onDelete = {
        self.deleteCell(indexPath)
    }
}
nadein
  • 357
  • 5
  • 14
  • Thanks for the solution. I followed your instruction and it still shows the same reaction... The button works well if I don't scroll. But, if I scroll back and forth twice, the button for the last cell does not trigger @IBAction as well. It triggers the function if I tap again (in the second tap). It's really strange... I've been spending lots of time trying to sort this out... your thoughts would be greatly appreciated. Thanks. – Gilbert Tyler Jun 22 '20 at 15:39