How to make prepareForSegue with UIcollectionView

后端 未结 3 979
耶瑟儿~
耶瑟儿~ 2021-01-13 08:50

I have problem with this code:

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    self.performSegue         


        
相关标签:
3条回答
  • 2021-01-13 09:18

    The following code should work well for you :

     override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    
        if segue.identifier == "showDetailsSegue" {
    
              let detailsVC = segue.destinationViewController as! DetailsViewController
              let cell = sender as! StampsCollectionViewCell
              let indexPaths = self.couponsCollectionView.indexPathForCell(cell)
              var thisCoupon = self.userCoupons[indexPaths!.row] as UserCoupons
              detailsVC.userCoupon = thisCoupon
              detailsVC.pinArray = self.pinArray
              detailsVC.userStamp = self.userStamps[indexPaths!.row]
    
        }
    
    }
    

    Update : Please check the incoming data from the server if its correct or not and it might work !

    0 讨论(0)
  • 2021-01-13 09:35

    You can try this:

    override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        let cell = collectionView.cellForItemAtIndexPath(indexPath)
        self.performSegueWithIdentifier("showDetailsSegue", sender: cell)
    }
    
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "showDetailsSegue" {
            let detailsVC: DetailsViewController = segue.destinationViewController as! DetailsViewController
            let cell = sender as! StampsCollectionViewCell
            let indexPath = self.collectionView!.indexPathForCell(cell)
            var thisCoupon = self.userCoupons[indexPath.row] as UserCoupons
            detailsVC.userCoupon = thisCoupon
            detailsVC.pinArray = self.pinArray
            detailsVC.userStamp = self.userStamps[indexPath.row]
    
        }
    
    }
    
    0 讨论(0)
  • 2021-01-13 09:38

    Try this:

    let cell = sender as StampsCollectionViewCell
    let indexPath = self.collectionView!.indexPathForCell(cell)
    

    And your code will be:

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) 
    
    {
        if segue.identifier == "showDetailsSegue" {
            let detailsVC: DetailsViewController = segue.destinationViewController as! DetailsViewController
            let cell = sender as StampsCollectionViewCell
            let indexPath = self.collectionView!.indexPathForCell(cell)
            var thisCoupon = self.userCoupons[indexPath.row] as UserCoupons
            detailsVC.userCoupon = thisCoupon
            detailsVC.pinArray = self.pinArray
            detailsVC.userStamp = self.userStamps[indexPath.row]
    
        }
    
    }
    

    Hope it will work.

    0 讨论(0)
提交回复
热议问题