Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Collection View Cell Button not triggering action

So I have a button that looks like a pencil in a collection view cell xib.

enter image description here

Then I have this code.

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "groupsCollectionViewCellIdentifier", for: indexPath) as! GroupCollectionViewCell

    //add action to the edit button
    cell.editButton.tag = indexPath.row
    cell.editButton.addTarget(self, action: #selector(GroupsCollectionViewController.editGroupAction), for: .touchUpInside)

    return cell
}

//segue to edit group
func editGroupAction() {
    performSegue(withIdentifier: "editGroupSegue", sender: self)
}

But whenever I click the edit button. Nothing is happening. I wonder what's missing.

like image 592
Chris Mikkelsen Avatar asked Dec 06 '16 06:12

Chris Mikkelsen


People also ask

How do I add a button in collection view?

Go to the Storyboard and drag a Right Bar Button Item to right side of the Navigation Bar of the Collection View Controller. Select the Bar Button Item and go to the Attributes inspector. Change the Custom Item Value to Add in the Bar Button Item section.

What is collection view delegate?

A collection view delegate manages user interactions with the collection view's contents, including item selection, highlighting, and performing actions on those items. The methods of this protocol are all optional. When configuring the collection view object, assign your delegate object to its delegate property.


2 Answers

I realize this ticket is pretty old, but I had the same problem, and found a different solution.

I ran Xcode with the view-debugger. While the button was sized correctly, it's superview had a width of 0 points. This would make it impossible to tap on this button, or any other subviews.

Just mentioning this, as it might be worth trying out.

like image 105
Dan Morrow Avatar answered Oct 20 '22 23:10

Dan Morrow


Try this one on your cell

cell.contentView.isUserInteractionEnabled = false
like image 26
Supanat Techasothon Avatar answered Oct 20 '22 22:10

Supanat Techasothon