如何在集合视图单元格中添加 uibutton 操作? [英] How to add uibutton action in a collection view cell?
本文介绍了如何在集合视图单元格中添加 uibutton 操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我有这个集合视图,其中包含位于右上角的编辑按钮的单元格.如何将动作连接到其中?
So I have this collection view with cells containing an edit button, located on the upper right corner. How do I wire up an action into it?
我尝试在 collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) 中添加
但是没有检测到 touchUpInside 事件.cell.editbutton.addTarget
->UICollectionViewCell
I tried adding cell.editbutton.addTarget
in collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
But it doesn't detect the touchUpInside Event.
推荐答案
你可能需要-
在cellForItemAtIndexPath
Swift 2.X
let editButton = UIButton(frame: CGRectMake(0, 20, 40,40))
editButton.setImage(UIImage(named: "editButton.png"), forState: UIControlState.Normal)
editButton.addTarget(self, action: #selector(editButtonTapped), forControlEvents: UIControlEvents.TouchUpInside)
cell.addSubview(editButton)
Swift 3.X
let editButton = UIButton(frame: CGRect(x:0, y:20, width:40,height:40))
editButton.setImage(UIImage(named: "editButton.png"), for: UIControlState.normal)
editButton.addTarget(self, action: #selector(editButtonTapped), for: UIControlEvents.touchUpInside)
cell.addSubview(editButton)
并执行您的操作-
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func editButtonTapped() -> Void {
print("Hello Edit Button")
}
这篇关于如何在集合视图单元格中添加 uibutton 操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文