简体   繁体   English

自定义CollectionView单元格PrepareForSegue

[英]Custom CollectionView Cell PrepareForSegue

I have a custom UICollection View Cell. 我有一个自定义的UICollection视图单元格。 One of that cell's property's is cellImageView . 该单元格的属性之一是cellImageView When the user selects a cell, I want to pass the image from that view ( cellImageView.image ) to the next one. 当用户选择一个单元格时,我想将图像从该视图( cellImageView.image )传递到下一个。 Here's what I've tried: 这是我尝试过的:

if let indexPath = self.collectionView?.indexPathForCell(sender as! UICollectionViewCell)
{
    if segue.identifier == "show"
    {
        var segue = segue.destinationViewController as! ViewPhoto
        segue.imageURL = URLToPass

    }
}

but quickly realized that's the wrong route to take. 但很快意识到这是错误的选择。 What's the best way to go about doing this? 这样做的最佳方法是什么? Thanks in advance. 提前致谢。

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) 
{ 
  if let cell = self.collectionView.cellForItemAtIndexPath(indexPath) as? GalleryClass
  {
    let imageToPass = cell.cellImageView.image
    self.performSegueWithIdentifier(mySegue, sender: imageToPass)
  }
}

And in your prepareForSegue function: 在您的prepareForSegue函数中:

if segue.identifier == "show"
{
  var segue = segue.destinationViewController as! ViewPhoto
  // segue.imageURL = URLToPass
  segue.image = sender as! UIImage
}

This way you're keeping code which contains CollectionView stuff in the own delegate method, which is easier to maintain if you have to change it in the future. 这样,您可以将包含CollectionView内容的代码保留在自己的委托方法中,如果将来需要更改它,则更易于维护。 And you only pass data in your prepareForSegue function which you actually need in there. 而且,您只在其中实际需要的prepareForSegue函数中传递数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM