gpt4 book ai didi

ios - 即使设置了委托(delegate),UICollectionView didSelectItemAtIndexPath 也从未调用过

转载 作者:行者123 更新时间:2023-11-29 12:13:16 25 4
gpt4 key购买 nike

我只是想在 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 没有被调用。我错过了什么?


看起来这可能是外行人的陷阱,但我仍然找不到它是什么。参见例如这个blog post .

最佳答案

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

关于ios - 即使设置了委托(delegate),UICollectionView didSelectItemAtIndexPath 也从未调用过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32704296/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com