简体   繁体   中英

Getting button action : UICollectionView Cell

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 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.

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];
}

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");
}

I've just solved it with adding

[self bringSubviewToFront:myButton];

into awakeFromNib

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 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.

在此输入图像描述

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];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM