如何快速在 HomeVC 中提供 xib 单元格按钮操作 [英] How to give xib cell button action in HomeVC in swift

查看:19
本文介绍了如何快速在 HomeVC 中提供 xib 单元格按钮操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了 xib collectionview 单元格..并且我能够在 HomeVC 中使用它的所有值,如下所示

I have created xib collectionview cell.. and i am able to use all its values in HomeVC like below

class HomeVC: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{

@IBOutlet var collectionView: UICollectionView!

 override func viewDidLoad() {

super.viewDidLoad()
let nib = UINib(nibName: "MainCollectionViewCell", bundle: nil)
collectionView.registerNib(nib, forCellWithReuseIdentifier: "MainCollectionViewCell")

 }

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

let cell = collectionView.dequeueReusableCellWithReuseIdentifier("MainCollectionViewCell", forIndexPath: indexPath) as! MainCollectionViewCell

 cell.arrivingLabel.text = indexData.arriv
 cell.lblDiscountedPrice.text = indexData.discPrice

 
return cell
}

像下面我可以给xib单元格按钮动作,但我想要在HomeVC中的xib单元格按钮动作如何,请在这里指导我

like below i can give action to xib cell button, but i want xib cell button action in HomeVC class how, please guide me here

   cell.btnDetails.addTarget(self, action: #selector(connected(sender:)), for: .touchUpInside)

   @objc func connected(sender: UIButton){
  }

我想要在 HomeVC 中这样

 @IBAction func productDetailsMain(_ sender: UIButton){
  }

注意:如果我使用相同的 collectionview 单元格,那么如果我从 HomeVC 按钮操作出口拖动到 collectionview 单元格按钮然后添加..但是如果我在 collectionview 中使用 xib 单元格,则此过程不起作用.. 如何在 HomeVC 类中给 xib 单元格按钮动作

note: if i use same collectionview cell then if i drag from HomeVC button action outlet to collectionview cell button then its adding.. but if i use xib cell in collectionview then this process is not working.. how to give xib cell button action in HomeVC class

推荐答案

如果想让集合视图单元格的按钮在拥有集合视图的视图控制器中触发一个动作,则需要添加视图控制器作为目标.您可以在 cellForItemAtIndexPath 中执行此操作,但您将需要删除之前的目标/操作,以免每次重复使用单元格时都继续添加新的目标/操作:

If you want to have the collection view cell's button trigger an action in the view controller that own's the collection view, you need to add the view controller as the target. You can do that in your cellForItemAtIndexPath, but you will need to remove the previous target/action so you don't keep adding a new target/action each time you reuse a cell:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("MainCollectionViewCell", forIndexPath: indexPath) as! MainCollectionViewCell

    cell.arrivingLabel.text = indexData.arriv
    cell.lblDiscountedPrice.text = indexData.discPrice
    // Remove the previous target/action, if any
    cell.btnDetails.removeTarget(nil, action: nil,for: .allEvents)
    // Add a new target/action pointing to the method `productDetailsMain(_:)`
    cell.btnDetails.addTarget(self, action: #selector(productDetailsMain(_:)), for: .touchUpInside)
    return cell
}

您也可以按照 luffy_064 的回答设置您的单元格以保持关闭.

You could also set up your cells to hold a closure as in luffy_064's answer.

请注意,如果您在 iOS 14 或更高版本上运行,则可以使用 UIControl 的新 addAction(_:for:) 方法将 UIAction 添加到您的按钮.(UIActions 包括一个闭包.)

Note that if you are running on iOS 14 or later, you can use the new addAction(_:for:) method of UIControl to add a UIAction to your button. (UIActions include a closure.)

可能看起来像这样:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("MainCollectionViewCell", forIndexPath: indexPath) as! MainCollectionViewCell

    cell.arrivingLabel.text = indexData.arriv
    cell.lblDiscountedPrice.text = indexData.discPrice
    // Remove the previous target/action, if any
        let identifier = UIAction.Identifier("button")

    removeAction(identifiedBy: identifier, for: .allEvents)
    // Add a new UIAction to the button for this cell
    let action = UIAction(title: "", image: nil, identifier: identifier, discoverabilityTitle: nil, attributes: [], state: .on) { (action) in
            print("You tapped button \(indexPath.row)")
            // Your action code here
        }
    cell(action, for: .touchUpInside)

    return cell
}

这篇关于如何快速在 HomeVC 中提供 xib 单元格按钮操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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