簡體   English   中英

自定義CollectionView單元格PrepareForSegue

[英]Custom CollectionView Cell PrepareForSegue

我有一個自定義的UICollection視圖單元格。 該單元格的屬性之一是cellImageView 當用戶選擇一個單元格時,我想將圖像從該視圖( cellImageView.image )傳遞到下一個。 這是我嘗試過的:

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

    }
}

但很快意識到這是錯誤的選擇。 這樣做的最佳方法是什么? 提前致謝。

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

在您的prepareForSegue函數中:

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

這樣,您可以將包含CollectionView內容的代碼保留在自己的委托方法中,如果將來需要更改它,則更易於維護。 而且,您只在其中實際需要的prepareForSegue函數中傳遞數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM