问题描述
我已经创建了 xib collectionview 单元格..并且我能够在 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 单元格按钮动作
解决方法
你必须使用闭包。
cell 类添加这个属性
import numpy as np
def calculate(num_list):
try:
num_array = np.array(num_list)
num_array = np.reshape(num_array,(3,3))
# If the number of elements in num_list
# is less than 9,np.reshape will return a
# ValueError
except ValueError:
print("List must contain nine numbers.")
return
# The problem is that after catching error with
# except ValueError:,the function continues
# and runs the three lines below.
# And so,since num_array is not a valid input,I get
# an axis value error.
mean_list.append(np.mean(num_array,axis=0).tolist())
mean_list.append(np.mean(num_array,axis=1).tolist())
mean_list.append(np.mean(num_array).tolist())
并在按钮操作上制作此
var connectionButtonAction: (() -> Void)?
所以最后在单元格创建中添加了这个:
@objc func connected(sender: UIButton){
connectionButtonAction?()
}
我认为这是一种简单的方法,但您也可以使用委托获得相同的方法。
,如果你想让集合视图单元格的按钮在拥有集合视图的视图控制器中触发一个动作,你需要添加视图控制器作为目标。您可以在 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
。 (UIActions 包括一个闭包。)
可能看起来像这样:
func collectionView(collectionView: UICollectionView,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
}