Chris Mikkelsen
Chris Mikkelsen

Reputation: 4087

How to add uibutton action in a collection view cell?

enter image description here

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 cell.editbutton.addTarget in collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell But it doesn't detect the touchUpInside Event.

Upvotes: 10

Views: 31251

Answers (6)

Janet Wang
Janet Wang

Reputation: 1

I also encountered the same problem and I tried the solution of putting the button.addTarget method inside the [cellForItemAtIndexPath] of the collectionView controller but it is not working, then I tried to have the following code put inside the init() from the UICollectionViewCell, then it worked fine. Hope this is helpful.

override init(frame: CGRect) {
        super.init(frame: frame)
editButton.addTarget(self, action: #selector(buttonPressed), 
for: .touchUpInside)
}

And have the action as following:

@objc func buttonPressed(){
    print("buttonPressed")
}

Upvotes: 0

iDeveloper
iDeveloper

Reputation: 2444

May be needful for you-

Write this code in 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)

And perform your action-

override func viewDidLoad() {
       super.viewDidLoad()
}

@IBAction func editButtonTapped() -> Void {
    print("Hello Edit Button")
}

Upvotes: 11

Mustafa Alqudah
Mustafa Alqudah

Reputation: 49

protocol CollectionViewCellDelegte {
    func collectionViewCellDelegte(didClickButtonAt index: Int)
}

class ImageCollectionViewCell: UICollectionViewCell {
    var index = 0
    var delegte: CollectionViewCellDelegte? = nil

    @IBAction func buttonAction(_ sender: UIButton) {
        if let del = self.delegte {
            del.collectionViewCellDelegte(didClickButtonAt: index)
        }
    }
}

Upvotes: 1

Surendra Kumar
Surendra Kumar

Reputation: 236

As I understood, you want to invoke edit button action instead of didSelectItemAt:indexPath. I solved this by override canBecomeFocused (return false), then your edit button action will be involved by tapping on edit button

Upvotes: -1

Himanshu
Himanshu

Reputation: 2842

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.

Upvotes: 27

Sayali Shinde
Sayali Shinde

Reputation: 96

On UICollectionView cell take button (Programmatically or using storyboard). Add action for that button in cell class file and declare delegate in that cell class file and call same delegate in button action method to perform edit action.

Then implement same delegate in ViewController in which you have created collection view

Upvotes: 1

Related Questions