[英]UICollectionView didSelectItemAtIndexPath never called even though delegate is set
我只是想在UIViewController
做一個普通的香草UICollectionView
。 一切都按預期工作,但當我觸摸CollectionViewCell
進行選擇時什么也沒有發生。 這是我的代碼的精髓:
@interface CreateNewFieldViewController () <UITextFieldDelegate, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
- (void) viewDidLoad {
...
UICollectionViewFlowLayout* flowLayout = [[UICollectionViewFlowLayout alloc]init];
flowLayout.itemSize = CGSizeMake(self.width*.15, self.width*.15);
[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
flowLayout.minimumInteritemSpacing = 30;
flowLayout.minimumLineSpacing = 30;
self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(self.width*.1, self.height*.2, self.width*.8, self.height*.3) collectionViewLayout:flowLayout];
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
self.collectionView.allowsSelection = YES;
self.collectionView.allowsMultipleSelection = NO;
self.collectionView.bounces = YES;
self.collectionView.alwaysBounceHorizontal = YES;
self.collectionView.alwaysBounceVertical = YES;
[self.collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
[self.view addSubview:self.collectionView];
self.collectionView.backgroundColor = [UIColor clearColor];
...
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
self.fieldNameInput.text = self.backgroundImageNames[indexPath.row];
NSLog(@"recording this: %@", self.fieldNameInput.text);
}
數據源和流布局工作正常,但是由於某些原因,當我單擊單元格時,什么也沒有發生。 我沒有看到任何日志語句,所以我知道未調用didSelect
。 我想念什么?
盡管我仍然找不到它,但看起來這可能是一個沒有經驗的人的陷阱。 參見例如此博客文章 。
額外調用UICollectionView可解決問題:
[collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
和
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
cell.selected = YES;
[collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
return cell;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.