场景: cell 中 button 点击的时候使用 闭包, 将点击事件 传到 对应的 vc
方法1:
- cell 中:
class MySignCell: UICollectionViewCell {
typealias ButtonClickBlock = (Int)->Void
var didClickButtonBlock: ButtonClickBlock?
@IBAction func remindButtonClick(_ sender: UIButton) {
if (didClickButtonBlock != nil) {
didClickButtonBlock(sender.tag)
}
}
override func awakeFromNib() {
super.awakeFromNib()
}
}
- 对应的 vc 中:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let mysign_cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MySignCell", for: indexPath) as! MySignCell
mysign_cell.didClickButtonBlock = { (button_tag) in
print(button_tag)
}
return mysign_cell
}
方法2:
- cell 中:
class MySignCell: UICollectionViewCell {
var addBtnClick:(_ sender:UIButton) -> () = { _ in }
@IBAction func remindButtonClick(_ sender: UIButton) {
addBtnClick(sender)
}
override func awakeFromNib() {
super.awakeFromNib()
}
}
- 对应的 vc 中:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let mysign_cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MySignCell", for: indexPath) as! MySignCell
mysign_cell.addBtnClick = { sender in
print (sender.tag)
}
return mysign_cell
}