Reputation: 20021
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?
Upvotes: 23
Views: 28269
Reputation: 1771
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];
}
Upvotes: 11
Reputation: 2209
I feel hard to understand the Accepted answer, I will try to give a simple answer.
In UICollectionViewCell there are two types.
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.
Upvotes: 0
Reputation: 3661
I've just solved it with adding
[self bringSubviewToFront:myButton];
into awakeFromNib
Upvotes: 2
Reputation: 55
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];
}
Upvotes: -3
Reputation: 71
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!
Upvotes: 1
Reputation: 2408
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.
Upvotes: 92
Reputation: 9091
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");
}
Upvotes: 7