I am looking way to get indexPath or data when i do longPress on cell. Basically i can to delete album from collectionView, to do that i need to get id.
My cellForItem
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "AlbumCollectionViewCell", for: indexPath) as! AlbumCollectionViewCell
cell.data = albumsDataOrigin[indexPath.row]
let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(self.longPressGetstureDetected))
cell.addGestureRecognizer(longPressGesture)
return cell
}
longPressGetstureDetected
@objc func longPressGetstureDetected(){
self.delegateAlbumView?.longPressGetstureDetected()
}
delete func
func longPressGetstureDetected() {
showAlertWith(question: "You wanna to delete this album?", success: {
self.deleteAlbum() //Here i need to pass ID
}, failed: {
print("Delete cenceled")
})
}
For people who looking complete answer
@objc func longPress(_ longPressGestureRecognizer: UILongPressGestureRecognizer) {
if longPressGestureRecognizer.state == UIGestureRecognizer.State.began {
let touchPoint = longPressGestureRecognizer.location(in: collectionView)
if let index = collectionView.indexPathForItem(at: touchPoint) {
self.delegateAlbumView?.longPressGetstureDetected(id: albumsDataOrigin[index.row].id ?? 0)
}
}
}