Adam Altinkaya
Adam Altinkaya

Reputation: 639

UICollectionView not calling didSelectItemAtIndexPath

I have a Collection View and has custom cells with images and labels in there. I have set my collection view as follows -

UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
flowLayout.minimumLineSpacing = 150.0f;
flowLayout.minimumInteritemSpacing = 104.0f;
flowLayout.sectionInset = UIEdgeInsetsMake(20, 20, 100, 120);

_archiveCollectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
_archiveCollectionView.frame = CGRectMake(30, 218, _archiveCollectionView.frame.size.width - 60, _archiveCollectionView.frame.size.height - 350);
_archiveCollectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
_archiveCollectionView.backgroundColor = [UIColor clearColor];
_archiveCollectionView.delegate = self;
_archiveCollectionView.dataSource = self;
[self.archiveCollectionView registerNib:[UINib nibWithNibName:@"FullArchiveEditionCell" bundle:nil] forCellWithReuseIdentifier:@"MyCell"];

[_archiveCollectionView reloadData];
[self.view addSubview:_archiveCollectionView];

I have also set the following methods:

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return _chosenCategoryArray.count;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [self addEditionsChildView];
}
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

However, my didSelectItemAtIndexPath never gets called when I select a cell. Any help please?

Upvotes: 1

Views: 8835

Answers (5)

maven25
maven25

Reputation: 261

Implement the below delegate method.

- (BOOL)collectionView:(UICollectionView *)collectionView shouldDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

And implement your

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
    //your selection management code 
}

and

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
    //deselection handling code 
}

Upvotes: 0

Lincker
Lincker

Reputation: 11

I have the same problem. And I solved by using storyboard to locate the collection cell in the collection view controller. Then tick User InterAction Enabled. I think using code to set in the UICollectionViewCell would also work. Hope it would help.

Upvotes: 1

gragera
gragera

Reputation: 309

I had a similar problem and it turned out I was using the same cell reuse identifier in two different collection views

Upvotes: 5

Natarajan
Natarajan

Reputation: 3271

In your header file have you implemented UICollectionViewDelegate as like below,

@interface HAViewController : UIViewController <UICollectionViewDataSource, UICollectionViewDelegate>

Upvotes: 1

jnix
jnix

Reputation: 480

Try with changing sequence as given below

[_archiveCollectionView reloadData];
[self.view addSubview:_archiveCollectionView];

to

[self.view addSubview:_archiveCollectionView];
[_archiveCollectionView reloadData];

Upvotes: 0

Related Questions