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?
I had a similar issue where subviews at the bottom part of the cell didn't receive touch events, but the top part was working fine. So I've started to investigate, and got the following results:
Setting the 'Autoresize subviews' of the cell in Interface Builder to YES solved my problem!
Create a Handle for the CollectionView in the UICollectionViewCell
In the .h file of the UICollectionViewCell
In the .m file of the UICollectionViewCell
Then in the implementation File of the Controller in the foll Method set the Collection View
Now in the implementation of your UICollectionViewCell
Now in your Button Clicked Method
UICollectionViewCell Class Reference
You can add your button in
awakeFromNib
like this:I've just solved it with adding
into
awakeFromNib
When you create a
UICollectionViewCell
via a nib the contents of the nib are not added to the cell's contentView -- it all gets added directly to theUICollectionViewCell
. There doesn't appear to be a way to get Interface Builder to recognize the top-level view in the nib as aUICollectionViewCell
, so all of the contents inside 'automatically' get added to the contentView.As sunkehappy pointed out, anything that you want to receive touch events needs to go into the contentView. It's already been created for you, so the best you can do is to programmatically move your
UIButton
into the contentView at awakeFromNib-time.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.
If it doesn't look like this, then its probably going to interpret touch events wrong.