How to add uibutton action in a collection view cell?

后端 未结 5 1043
走了就别回头了
走了就别回头了 2021-01-03 01:03

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?

I tried adding <

5条回答
  •  伪装坚强ぢ
    2021-01-03 01:34

    Create the outlet of your UIButton in UICollectionViewCell, write In

    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)
    }
    

    Make sure userInteraction is enable for the button as well as for UICollectionViewCell.

提交回复
热议问题