在Cell之外获取CollectionViewCell的IndexPath

时间:2017-06-03 13:30:27

标签: ios swift swift3 indexpath

首先,我是一个快速的新手,所以要温柔。我在获取单元格外的单元格的indexPath时遇到问题。我想获取函数的索引路径并使用它将内容发送到另一个视图。图像是在单元格中以编程方式创建的,它有一个轻击手势识别器发送到此功能(其中" myIndex"定义为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)


}

我知道我可以在IndexPath使用didSelectItem,我也有myIndex的声明,但我有一些东西会从那里的segue中触发:

//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)

}

我对segue的表现如下:`override

func prepare(对于segue:UIStoryboardSegue,发件人: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

}

所以我的问题是,如何在不使用prepareForSegue的情况下将这两个字段传递给另一个视图?我现在正在努力奋斗20个小时,仍然无法理解。

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

更新:感谢Jose的回答让它工作,现在它看起来像这样......以防任何人遇到与我相同的问题。

 // 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 个答案:

答案 0 :(得分:3)

获取tapGestureRecognizercollectionView的位置,然后获取指定位置的indexPath

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

答案 1 :(得分:1)

我认为这对单元格的代表来说很容易,但有点复杂。

简单的答案应该是将索引存储在tapped图像的imageView的“tag”属性中,然后从gestureRecognizer中提取 您可以在创建imageView的位置添加标记,恢复标记的代码如下所示:

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)


}

答案 2 :(得分:0)

如果您的代码如下所示,请尝试此

行方法的集合视图单元格

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

你的功能在集合视图外面

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