2014-12-13 66 views
1

我试图将信息发送到关于所选单元格的详细视图。现在,prepareForSegue在我使用的集合视图委托方法之前运行。这导致我发送前一个单元格选择的信息而不是当前单元格的信息。通过CollectionViewCell选择发送信息到详细视图

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 
    collectionView.deselectItemAtIndexPath(indexPath, animated: true)   
    nextScreenRow = indexPath.row 

    self.performSegueWithIdentifier("toDetails", sender: self) 
} 

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) { 
    if segue.identifier == "toDetails" { 
     let vc = segue.destinationViewController as HistoryDetailsViewController 
     vc.postcard = postcards[nextScreenRow] 
    } 
} 
+0

是否有可能,你必须从'collectionViewCell'到'HistoryDe​​tailsViewController'一个SEGUE,并从当前视图控制器,它的另一个SEGUE也。 – gabbler 2014-12-13 02:50:46

+0

“toDetails”是从collectionViewCell到HistoryDe​​tailsViewController的继承。我在当前的视图控制器中也有一个展开顺序。 – Jerrod 2014-12-13 03:02:58

+1

“toDetails”在故事板中定义,因此,您不必手动调用'performSegueWithIdentifier'来执行两次。 – gabbler 2014-12-13 03:11:19

回答

1

两件事。如果segue是由cell创建的,那么你不应该在code中调用performSegue;选择单元格将触发无代码的继续。其次,当你以这种方式连接一个segue时,你根本不需要实现didSelectItemAtIndexPath(但是如果你只是想调用deselectItemAtIndexPath就可以)。不需要它;你可以在prepareForSegue中做你需要的一切。该电池将寄件人,因此可以做这个,

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) { 
    if segue.identifier == "toDetails" { 
     let cell = sender as UICollectionViewCell 
     let indexPath = collectionView!.indexPathForCell(cell) 
     let vc = segue.destinationViewController as HistoryDetailsViewController 
     vc.postcard = postcards[indexPath.item] 
    } 
} 
相关问题