简体   繁体   中英

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?

我有一个类似的问题,原来我在两个不同的集合视图中使用相同的单元重用标识符

在头文件中,您实现了如下的UICollectionViewDelegate

@interface HAViewController : UIViewController <UICollectionViewDataSource, UICollectionViewDelegate>

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.

Try with changing sequence as given below

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

to

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

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 
}

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