UICollectionView不调用didSelectItemAtIndexPath

时间:2014-03-21 17:14:59

标签: ios objective-c uicollectionview uicollectionviewcell

我有一个集合视图,并在其中包含带有图像和标签的自定义单元格。我已将集合视图设置如下 -

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

我还设置了以下方法:

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

然而,当我选择一个单元格时,我的didSelectItemAtIndexPath永远不会被调用。有什么帮助吗?

5 个答案:

答案 0 :(得分:5)

我遇到了类似的问题,结果发现我在两个不同的集合视图中使用了相同的单元格重用标识符

答案 1 :(得分:1)

在您的标题文件中,您实施了UICollectionViewDelegate,如下所示

@interface HAViewController : UIViewController <UICollectionViewDataSource, UICollectionViewDelegate>

答案 2 :(得分:1)

我有同样的问题。我通过使用故事板在集合视图控制器中找到集合单元来解决。然后勾选启用用户InterAction 。我认为在UICollectionViewCell中使用代码设置也可以。希望它会有所帮助。

答案 3 :(得分:0)

尝试更改顺序,如下所示

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

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

答案 4 :(得分:0)

实现以下委托方法。

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

实施你的

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

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