如何在集合视图单元格中添加 uibutton 操作?

2023-11-23

enter image description here

所以我有这个集合视图,其中的单元格包含位于右上角的编辑按钮。我如何将一个动作连接到其中?

我尝试添加cell.editbutton.addTarget in collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell但它没有检测到 touchUpInside 事件。


在 UICollectionViewCell 中创建 UIButton 的插座,写入

func collectionView(_ collectionView: UICollectionView, 
                    cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    cell.button.tag = indexPath.row
    cell.button.addTarget(self, 
        action: #selector(self.yourFunc(), 
        for: .touchUpInside)
}

func yourFunc(sender : UIButton){
    print(sender.tag)
}

确保为按钮和 UICollectionViewCell 启用了 userInteraction。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在集合视图单元格中添加 uibutton 操作? 的相关文章

随机推荐