簡體   English   中英

UICollectionView不調用didSelectItemAtIndexPath

[英]UICollectionView not calling didSelectItemAtIndexPath

我有一個“收藏夾視圖”,並具有其中包含圖像和標簽的自定義單元格。 我將收藏視圖設置如下:

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永遠不會被調用。 有什么幫助嗎?

我有一個類似的問題,原來我在兩個不同的集合視圖中使用相同的單元重用標識符

在頭文件中,您實現了如下的UICollectionViewDelegate

@interface HAViewController : UIViewController <UICollectionViewDataSource, UICollectionViewDelegate>

我也有同樣的問題。 我通過使用情節提要板在集合視圖控制器中定位集合單元來解決。 然后勾選“ 用戶交互已啟用” 我認為使用代碼在UICollectionViewCell中進行設置也可以。 希望這會有所幫助。

嘗試更改順序,如下所示

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

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

實現以下委托方法。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM