Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make prepareForSegue with UIcollectionView

I have problem with this code:

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

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

    }

}

When I have only one item in collection view I can't go to DetailsViewController. When I have few items, my segue works with items without last item.

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return self.userCoupons.count
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    var cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! StampsCollectionViewCell

    let thisStamp = self.userCoupons[indexPath.row]

    if thisStamp.couponImage != nil {
        var image = thisStamp.couponImage
        cell.stampImage.image = image
    }

    var day: NSString = (thisStamp.couponDate as NSString).stringByReplacingCharactersInRange(NSRange(location: 0, length: 6), withString: "")
    var monthS: NSString = (thisStamp.couponDate as NSString).stringByReplacingCharactersInRange(NSRange(location: 0, length: 4), withString: "")
    var month:  NSString = monthS.stringByReplacingCharactersInRange(NSRange(location: 2, length: 2), withString: "")
    var year: NSString = (thisStamp.couponDate as NSString).stringByReplacingCharactersInRange(NSRange(location: 4, length: 4), withString: "")

    cell.stampDate.text = "\(day).\(month).\(year)"
    return cell
}
like image 269
Maselko Avatar asked Aug 10 '15 07:08

Maselko


3 Answers

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 !

like image 63
AaoIi Avatar answered Oct 17 '22 01:10

AaoIi


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.

like image 27
Dharmesh Kheni Avatar answered Oct 17 '22 01:10

Dharmesh Kheni


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]

    }

}
like image 26
Bannings Avatar answered Oct 17 '22 00:10

Bannings