Getting button action : UICollectionView Cell

Lithu T.V picture Lithu T.V · Dec 7, 2012 · Viewed 27.8k times · Source

I have created a UICollectionViewCell by nib and added a button inside it and created a .h and .m files added the class to the nibs file's owner.then wrote a button action in the .m connected it via outlet.

The collection view is populating fine ,but cannot get the buton action triggered. I think the delegate for collection cell is called.

How can i get the button action?

Answer

haider picture haider · Jul 15, 2013

I had this problem as well. No subviews would receive touch events. While Scott K's workaround does work, I still felt something was wrong. So I took another look at my nib, and noticed that the original subview I used to create a UICollectionViewCell was a UIView. Even though I changed the class to a subclass of UICollectionViewCell, XCode still considered it a UIView, and hence the issues you see with contentView not catching touch events.

To fix this, I redid the nib by making sure to drag a UICollectionViewCell object, and moving all the subviews to that. Afterwards, touch events began to work on my cell's subviews.

Could indicator to see if your nib is configured as a UICollectionViewCell is look at the icon for your high level view.

enter image description here

If it doesn't look like this, then its probably going to interpret touch events wrong.