goldsdad answer is perfect it also usable for UICollectionViewCell added code below
`@objc func buttonClick(_ sender: UIButton) {
var superview = sender.superview
while let view = superview, !(view is UICollectionViewCell) {
superview = view.superview
}
guard let cell = superview as? UICollectionViewCell else {
print("button is not contained in a table view cell")
return
}
guard let indexPath = collectionView.indexPath(for: cell) else {
print("failed to get index path for cell containing button")
return
}
// We've got the index path for the cell that contains the button, now do something with it.
print("button is in row (indexPath.row)")
}`