2016-07-08 55 views
0

問題是這樣的線回零:UICollectionView prepareForSegue indexPathForCell爲零的iOS 9

if let indexPath = self.collectionView?.indexPathForCell(sender as! UICollectionViewCell) 

出於這個原因,我不能傳遞任何信息,該標識符被設置,該委託被設置。

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    if segue.identifier == "DetalleSegueIdentifier" { 
     if let indexPath = self.collectionView?.indexPathForCell(sender as! UICollectionViewCell) { 
      let detailVC = segue.destinationViewController as! DetalleNoticiaViewController 
      detailVC.contact = self.noticia[indexPath.row] 
     } 
    } 
} 

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 
    self.performSegueWithIdentifier("DetalleSegueIdentifier", sender: collectionView.cellForItemAtIndexPath(indexPath)) 
} 

任何幫助?

+0

你在哪裏打電話給你的segue?在按鈕上按?在細胞新聞? –

+0

在單元格上按 –

+0

請顯示你調用的代碼部分performSegueWithIdentifier –

回答

0

問題是變種的CollectionView是不是在我的ViewController引用這個原因總是返回零。我的錯誤感謝你的答案。

@IBOutlet weak var collectionView: UICollectionView! 
1

問題是如何嘗試恢復單元格。您必須在performSegueWithIdentifier中傳遞單元格,然後在prepareForSegue中恢復它。 下面是代碼:

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 
    self.performSegueWithIdentifier("DetalleSegueIdentifier", sender: collectionView.cellForItemAtIndexPath(indexPath)) 
} 

然後

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    if segue.identifier == "DetalleSegueIdentifier" { 
     if let cell = sender as? UICollectionViewCell{ 
      let indexPath = self.collectionView!.indexPathForCell(cell) 
      let detailVC = segue.destinationViewController as! DetalleNoticiaViewController 
      detailVC.contact = self.noticia[indexPath.row] 

     } 

    } 
} 

希望它能幫助!

+0

不要工作同樣的問題是無 –

1

嘗試這樣的..

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 
     self.performSegueWithIdentifier("DetalleSegueIdentifier", sender: indexPath) 
    } 

,讓你的indexpath這樣的...

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
     if segue.identifier == "DetalleSegueIdentifier" { 

      let aIndPath = sender as! NSIndexPath 

      let detailVC = segue.destinationViewController as! DetalleNoticiaViewController 
      detailVC.contact = self.noticia[aIndPath.item] 
     } 
    } 
+0

不要工作同樣的問題是零 –