23

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?

Lithu T.V
  • 19,955
  • 12
  • 56
  • 101

7 Answers7

92

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.

haider
  • 2,418
  • 3
  • 24
  • 26
  • Works for me too. But I wonder if this breaks anything at all internally. – pixelfreak Aug 10 '13 at 01:50
  • Migrated from PSTCollectionView to UICollectionView and had this problem. – BastiBen Sep 21 '13 at 00:46
  • @haider: Thank you so much for providing this answer. I had organized my storyboard and nib in the same manner and experienced the same trouble. It is very easy to overlook something like this when a UIView is inserted to a new nib by default. I am very grateful for your answer. – Coach Roebuck Dec 02 '13 at 18:56
  • This is also the case if a button is a subview on a UIView! Make sure the button is on the top level! – AJ9 Aug 01 '14 at 16:06
  • Great! I didn't want to apply all my constraints again so I fiddled with the xib file and converted it to a `UICollectionViewCell` without losing my constraints. – Koen. Oct 19 '14 at 17:10
  • @AJ9: Actually - A button inside a UIView is fine - SO LONG AS - the UIView has 'isUserInteractionEnabled = true' – Stephen Orr Jul 01 '18 at 16:15
11

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 the UICollectionViewCell. There doesn't appear to be a way to get Interface Builder to recognize the top-level view in the nib as a UICollectionViewCell, 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.

-(void)awakeFromNib {
    [self.contentView addSubview:self.myButton];
}
R. Mohan
  • 2,182
  • 17
  • 30
Scott K.
  • 1,761
  • 15
  • 26
  • I don't know about previous versions, but in Xcode 5.1.1, creating a nib with a UICollectionViewCell as the root view element, will result in adding anything added to that root view to the contentView of the final cell once loaded. Thus, the part of this answer where it is stated that the contents of a nib will be added to the UICollectionViewCell instead of its contentView, is incorrect (at least for Xcode 5.1.1). – Lukas Kalinski Jun 27 '14 at 09:42
  • Is there a way to somehow implement the index path by using this solution? – Bryan P Jul 07 '14 at 07:13
7

UICollectionViewCell Class Reference

To configure the appearance of your cell, add the views needed to present the data item’s content as subviews to the view in the contentView property. Do not directly add subviews to the cell itself. The cell manages multiple layers of content, of which the content view is only one. In addition to the content view, the cell manages two background views that are display the cell in its selected and unselected states.

You can add your button in awakeFromNib like this:

- (void)awakeFromNib
{
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
    [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:button];
}

- (void)buttonClicked:(id)sender
{
    NSLog(@"button clicked");
}
sunkehappy
  • 8,970
  • 5
  • 44
  • 65
2

I've just solved it with adding

[self bringSubviewToFront:myButton];

into awakeFromNib

kalafun
  • 3,512
  • 6
  • 35
  • 49
  • 2
    It's not working but adding the target selector by code in awakeFromNib works perfectly. – Medhi Oct 11 '16 at 12:11
1

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:

  • The interface builder will add any subviews of the cell you create in it to the contentView of the cell, even though the contentView itself is not visible in the interface builder
  • My code expanded the cells to suit the size of the content, so the majority of the cells in the collection view were of larger height than the blueprint in Interface Builder
  • For some reason, the 'Autoresize subviews' property of the cell itself was set to NO. This caused mysterious and not-visible-in-interface-builder contentView to remain of the same size as the cell had in interface builder originally, so any subviews that were outside the bounds of the contentView did not receive touches and were unresponsive

Setting the 'Autoresize subviews' of the cell in Interface Builder to YES solved my problem!

  • I already had "Autoresize subviews" set to YES, but your answer showed me that indeed my contentView wasn't being resized properly, and this was causing the problem. So now I need to figure out what other than "Autoresize subviews" could be causing this... EDIT: This answer solved it for me: http://stackoverflow.com/a/25774492/555807 Turns out it was the result of a bug in switching from Xcode 5 to Xcode 6. – SeanR Sep 16 '14 at 11:02
0

I feel hard to understand the Accepted answer, I will try to give a simple answer.

In UICollectionViewCell there are two types.

  1. Collection View Cell
  2. Collection Reusable View

I used the Collection Reusable View, in that the button actions are not working.

Then as per the accepted answer i tried to use the Collection View Cell, in that only the Button Actions are Working. Use the second object in the Image. It will work fine.

enter image description here

R. Mohan
  • 2,182
  • 17
  • 30
-3

Create a Handle for the CollectionView in the UICollectionViewCell

In the .h file of the UICollectionViewCell

@property (nonataomic, retain) UICollectionView *collView;

In the .m file of the UICollectionViewCell

@synthesize *collView;

Then in the implementation File of the Controller in the foll Method set the Collection View

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
  YourCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:homePageCollViewCellIdentifier forIndexPath:indexPath];
    //NSString *str = [NSString stringWithFormat:@"HP item %d", indexPath.row+1];
    cell.collView = self.theCollectionView;
}

Now in the implementation of your UICollectionViewCell

- (void)awakeFromNib
{
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
    [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:button];
}

Now in your Button Clicked Method

-(void)buttonClicked:(id)sender
{
    NSLog(@"button clicked");
    NSIndexPath *indPath = [collVw indexPathForCell:self];    
    [collVw.delegate collectionView:self.collVw didSelectItemAtIndexPath:indPath];
}
leolobato
  • 2,359
  • 3
  • 32
  • 51
  • It's a bad idea to conflate `-collectionView:didSelectItemAtIndexPath:` with button presses, never mind having every cell know about its parent collection view – fatuhoku Sep 12 '15 at 13:44
  • You are also creating a strong retain cycle between the cell and the collection view – gurooj Oct 13 '16 at 00:02