2014-12-13 63 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] 
    } 
} 
相關問題