2015-05-28 46 views
0

我有一個自定義的UICollection View Cell。其中一個單元的財產是cellImageView。當用戶選擇一個單元格時,我想將該圖像從該視圖(cellImageView.image)傳遞到下一個單元格。以下是我試過的:Custom CollectionView Cell PrepareForSegue

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

    } 
} 

但很快意識到這是錯誤的路線。要做到這一點,最好的方法是什麼?提前致謝。

+0

看起來對我來說很好,你認爲這是錯誤的嗎? – Icaro

+0

看起來也在我身上發現,除了變量名稱segue,這是不明確的。 –

+0

走這條路之後,有沒有辦法讓cellImageView.image –

回答

0
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函數中的數據。

+0

謝謝你這個作品! –

+0

不客氣! – ezcoding

+1

任何使用'didDeselectItemAtIndexPath'而不是'didSelecteItemAtIndexPath'的理由? – SeNeO