Get IndexPath CollectionViewCell outside of cell

First of all, I'm new to fast, so be careful. I have a problem with selecting a indexPath

cell outside a cell. I want to get this function index path and use it to post content to another view. The image was created programmatically in a cell, it has a gesture recognizer sending this function (where " myIndex

" is defined as Int : var myIndex = 0

):

// tap image to go at the indexpath of the user
func imageTapped(tapGestureRecognizer: UITapGestureRecognizer) {
   print("image tapped")

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let secondViewController = storyboard.instantiateViewController(withIdentifier: "patientProfile") as! PatientProfileVC
    secondViewController.userID = posts[myIndex].userID
    secondViewController.patientName = posts[myIndex].author

    print("Passed data to the other view")

    self.show(secondViewController, sender: self)


}

      

I know that I could use didSelectItem in IndexPath where I also have a declaration myIndex

, but I have something to be called from a segue there:

//standard functon for item selected
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    //get the index of the cell
    myIndex = indexPath.row

    print("Cell is selected")

    //perform the segue
    self.performSegue(withIdentifier: "CommentsVC", sender: self)
    print(myIndex)

}

      

and my execution for segue looks like this: `override

func prepare (for segue: UIStoryboardSegue, sender: any?) {

    print("Segue Initiated")
    feed.navigationItem.title = nil

    //if segue.destination is CommentsFullVC{
        print("Comments segue initiated")
        let secondViewController = segue.destination as! CommentsFullVC
        secondViewController.questionData = posts[myIndex].postContent
        secondViewController.authorName = posts[myIndex].author
        secondViewController.date = posts[myIndex].normalDate
        secondViewController.profileImagePath = posts[myIndex].pathToImage
        secondViewController.typeOfClinic = posts[myIndex].clinic
        secondViewController.postID = posts[myIndex].postID
        secondViewController.followersForPost = posts[myIndex].followers

}

      

So my question is, how can I pass these 2 fields to another view without using prepareForSegue? I've been struggling with this for 20 hours and still can't figure it out.

secondViewController.userID = posts[myIndex].userID
secondViewController.patientName = posts[myIndex].author

      

UPDATE: Got it working thanks to Jose's answer, now it looks like this ... in case someone else has the same problem as mine.

 // tap image to go at the indexpath of the user
func imageTapped(tapGestureRecognizer: UITapGestureRecognizer) {
   print("image tapped")


    let pointInCollectionView = tapGestureRecognizer.location(in: collectionView)
    let indexPath = collectionView?.indexPathForItem(at: pointInCollectionView)
    print(indexPath!)

    // this one works - show segue
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let secondViewController = storyboard.instantiateViewController(withIdentifier: "patientProfile") as! PatientProfileVC
    secondViewController.userID = posts[(indexPath?.row)!].userID
    secondViewController.patientName = posts[(indexPath?.row)!].author

    print("Passed data to the other view")

    self.show(secondViewController, sender: self)


}

      

+3


source to share


3 answers


Get location tapGestureRecognizer

at collectionView

and then indexPath

at a given location



let pointInCollectionView = tapGestureRecognizer.location(in: collectionView)
let indexPath = collectionView.indexPathForItem(at: pointInCollectionView)

      

+2


source


I think it would be easy with a cell delegate, but it's a bit tricky.

An easy answer would be to store the index in the "tag" property of the Viewed image of the rendered image and then retrieve it from the gestureRecognizer You can add the tag where you create the imageView and the code where you restore the tag will look like this:



func imageTapped(tapGestureRecognizer: UITapGestureRecognizer) {
    print("image tapped")
    var myIndex = 0
    if let imageView = tapGestureRecognizer.view {
        myIndex = imageView.tag
    }

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let secondViewController = storyboard.instantiateViewController(withIdentifier: "patientProfile") as! PatientProfileVC
    secondViewController.userID = posts[myIndex].userID
    secondViewController.patientName = posts[myIndex].author

    print("Passed data to the other view")

    self.show(secondViewController, sender: self)


}

      

+1


source


If your code writes like below try this

collection view cell for row Method

cell.image = indexPath.row
let tap = UITapGestureRecognizer.init(target: self, action: #selector(imageTapped(_:)))
tap.accessibilityElements = [indexPath]
cell.image.addGestureRecognizer(tap)

      

Your function side view of the collection

func imageTapped (_ sender:UITapGestureRecognizer) {
     let index = sender.accessibilityElements?.first as! IndexPath   
     print(index.row)
     myIndex = index.row
}

      

0


source







All Articles