简体   繁体   English

从集合视图单元执行 Segue

[英]Perform Segue from Collection View Cell

import UIKit

class ActionCollectionViewCell: UICollectionViewCell {
     @IBOutlet weak var myLabel: UILabel!
     @IBOutlet weak var actionGIF: UIImageView!

     @IBAction func actionPressed(sender: AnyObject) {
         print(myLabel.text)
         Global.actionButtonIndex = myLabel.text!.toInt()! - 1
         print(actionGIF.image)
         ActionViewController.performSegueWithIdentifier("showActionPreview", sender: nil)
}

} }

I am trying to perform a Segue after User Clicking on One of the Cell in my Collection View.我试图在用户单击我的集合视图中的一个单元格后执行 Segue。 Can't seem to do that using performSegueWithIdentifier.使用 performSegueWithIdentifier 似乎无法做到这一点。 App Screenshot应用截图

Here's an elegant solution that only requires a few lines of code:这是一个优雅的解决方案,只需要几行代码:

  1. Create a custom UICollectionViewCell subclass创建自定义 UICollectionViewCell 子类
  2. Using storyboards, define an IBAction for the " Touch Up Inside " event of your button使用故事板,为按钮的“ Touch Up Inside ”事件定义一个 IBAction
  3. Define a closure定义一个闭包
  4. Call the closure from the IBAction从 IBAction 调用闭包

Swift 4+ code Swift 4+代码

class MyCustomCell: UICollectionViewCell {

        static let reuseIdentifier = "MyCustomCell"

        @IBAction func onAddToCartPressed(_ sender: Any) {
            addButtonTapAction?()
        }

        var addButtonTapAction : (()->())?
    }

Next, implement the logic you want to execute inside the closure in your接下来,在你的闭包中实现你想要执行的逻辑

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: MyCustomCell.reuseIdentifier, for: indexPath) as? MyCustomCell else {
            fatalError("Unexpected Index Path")
        }

        // Configure the cell
        // ...


        cell.addButtonTapAction = {
            // implement your logic here, e.g. call preformSegue()  
            self.performSegue(withIdentifier: "your segue", sender: self)              
        }

        return cell
    }

You can use this approach also with table view controllers.您也可以将此方法用于表视图控制器。

Instance method performSegue is not available from a UICollectionViewCell :实例方法performSegueUICollectionViewCell不可用:

Since an UICollectionViewCell is not an UIViewController , you can not use performSegue(withIdentifier:sender:) from it.由于UICollectionViewCell不是UIViewController ,因此您不能从中使用performSegue(withIdentifier:sender:) You may prefer use delegates to notify your parent view controller and then, performSegue from there.您可能更喜欢使用委托来通知您的父视图控制器,然后从那里执行performSegue

Take a look at the details of this answer .看看这个答案的细节。 The question is slightly different but the solution lies in the same pattern.问题略有不同,但解决方案在于相同的模式。

Have you set the segue identifier by exactly named "showActionPreview".您是否通过完全命名的“showActionPreview”设置了 segue 标识符。 Moreover, ensure that your segue linked from your parent view controller to your destination view controller in storyboard.此外,请确保您的 segue 从您的父视图控制器链接到故事板中的目标视图控制器。 Hope this would be help.希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM