我对此代码有疑问:
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]
}
}
当我在集合视图中只有一个项目时,我无法访问DetailsViewController。当我的物品很少时,我的segue可以处理没有最后一项的物品。
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
}
答案 0 :(得分:5)
以下代码应该适合您:
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]
}
}
更新:请检查来自服务器的传入数据是否正确,它可能会有效!
答案 1 :(得分:0)
试试这个:
let cell = sender as StampsCollectionViewCell
let indexPath = self.collectionView!.indexPathForCell(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]
}
}
希望它能奏效。
答案 2 :(得分:0)
你可以试试这个:
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]
}
}