gpt4 book ai didi

swift - 如何在 Collection View 单元格中添加 uibutton 操作?

转载 作者:搜寻专家 更新时间:2023-10-31 08:09:13 25 4
gpt4 key购买 nike

enter image description here

所以我有这个 Collection View ,其中的单元格包含一个编辑按钮,位于右上角。如何将 Action 连接到其中?

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

最佳答案

在UICollectionViewCell中创建你的UIButton的outlet,写在

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。

关于swift - 如何在 Collection View 单元格中添加 uibutton 操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41456441/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com