如何在集合视图单元格中添加uibutton动作? [英] How to add uibutton action in a collection view cell?

查看:156
本文介绍了如何在集合视图单元格中添加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) -> UICollectionViewCell中添加cell.editbutton.addTarget,但是没有检测到touchUpInside事件.

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屋!

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