如何使用 UIcollectionView 制作 prepareForSegue [英] How to make prepareForSegue with UIcollectionView

查看:23
本文介绍了如何使用 UIcollectionView 制作 prepareForSegue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对这段代码有疑问:

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 可以处理没有最后一个项目的项目.

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 !

这篇关于如何使用 UIcollectionView 制作 prepareForSegue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆