2016-07-08 56 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

不要工作同样的问题是零 –