【问题标题】:How to make prepareForSegue with UIcollectionView如何使用 UIcollectionView 制作 prepareForSegue
【发布时间】: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 项有错误。

标签: swift uicollectionview


【解决方案1】:

下面的代码应该很适合你:

 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]

    }

}

更新:请检查从服务器传入的数据是否正确,它可能会起作用!

【讨论】:

  • 仍然无法正常工作:无法将“AppTest.SecondViewController”(0x1043047f0)类型的值转换为“MyCollectionViewCell”(0x105ea1008)。
  • 你有自定义单元格吗?
  • 是的。命名:MyCollectionViewCell
  • 然后将 CollectionViewCell 更改为 MyCollectionViewCell 并告诉我!
  • 无法将“AppTest.SecondViewController”(0x1016c8940)类型的值转换为“AppTest.StampsCollectionViewCell”(0x1016c75b0)。绿色 SIGABRT。
【解决方案2】:

你可以试试这个:

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]

    }

}

【讨论】:

  • 无法将“AppTest.SecondViewController”(0x10c30c940)类型的值转换为“NSIndexPath”(0x10cd5c680)。
  • 发件人是cell 而不是self
  • 绿色错误:let cell = sender as! StampsCollectionViewCell 和控制台中无法将“AppTest.SecondViewController”(0x1014c5950)类型的值转换为“AppTest.StampsCollectionViewCell”(0x1014c45c0)。
  • 如何在情节提要中设置showDetailsSegue segue?
  • 是的,我有。 Segue 效果很好。我可以从集合视图索引的 DetailsVC 项目中看到:0、1、2。但我有 4 个项目,最后一个项目生成错误。
【解决方案3】:

试试这个:

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]

    }

}

希望它会起作用。

【讨论】:

  • 绿色 SIGABRT on let cell = sender as! StampsCollectionViewCell 和无法将“AppTest.SecondViewController”(0x10c9d8940)类型的值转换为“AppTest.StampsCollectionViewCell”(0x10c9d75b0)。
  • 你能分享示例项目吗?
  • 什么是StampsCollectionViewCell
  • 这是我的自定义 CollectionViewCell
  • 发件人也是 StampsCollectionViewCell 天哪!
猜你喜欢
  • 1970-01-01
  • 2020-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多