【问题标题】:How to give xib cell button action in HomeVC in swift如何在 HomeVC 中快速提供 xib 单元格按钮操作
【发布时间】:2021-09-26 14:24:42
【问题描述】:

我已经创建了 xib 集合视图单元.. 我可以在 HomeVC 中使用它的所有值,如下所示

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 单元格按钮操作如何,请在此处指导我

   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 单元格按钮操作

【问题讨论】:

    标签: swift button action cell xib


    【解决方案1】:

    你必须使用闭包。

    cell 类添加这个属性

    var connectionButtonAction: (() -> Void)?
    

    在按钮动作上做这个

    @objc func connected(sender: UIButton){
        connectionButtonAction?()
      }
    

    所以最后在单元格创建中你添加了这个:

    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
    
     
    cell.connectionButtonAction = { [weak self] in
       print("cell button pressed")
    }
    return cell
    }
    

    我认为这是一种简单的方法,但您也可以使用委托获得相同的方法。

    【讨论】:

      【解决方案2】:

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

      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 的回答那样设置你的细胞来保持闭合。

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

      可能看起来像这样:

      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
      }
      

      【讨论】:

      • 我想在 viewcontroller 类中使用单元格的按钮操作,如何?
      • 赞这个@IBAction func productDetailsMain(_ sender: UIButton){ }
      • 我的回答解释了这一点。在您的视图控制器类中,将目标/操作分配给您的 cellForItemAtIndexPath 方法中的按钮。 (但请务必删除以前的目标/动作分配,以免继续添加更多内容,并为每次按钮点击多次调用 IBAction 方法。)
      猜你喜欢
      • 2021-09-26
      • 1970-01-01
      • 2021-04-25
      • 1970-01-01
      • 2020-03-27
      • 1970-01-01
      • 1970-01-01
      • 2022-06-14
      • 2018-05-25
      相关资源
      最近更新 更多