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
}
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 !
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.
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]
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With