【发布时间】:2015-08-10 07:48:33
【问题描述】:
我对这段代码有疑问:
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
}
【问题讨论】:
-
最后一项还是不行。
-
那行有错误吗?
-
我这里有错误:let indexPaths = self.couponsCollectionView.indexPathsForSelectedItems() EXC_BAD_INTRODUCTION 绿色错误。我有 3 个项目。第 1 项和第 2 项效果很好,我只有第 3 项有错误。