ios – 在点击非可选单元格后取消选择先前选择的UICollectionViewCells

我正在实现一个启用了多个选择的UICollectionView.

我的一些细胞是可选择的,有些则不是.这是一系列事件:

>我通过点击它们并选择YES来选择几个单元格

> shouldHighlightItemAtIndexPath:
> shouldSelectItemAtIndexPath:

>我尝试通过点击它来选择一个不可选择的单元格(通过将NO返回给shouldSelectItemAtIndexPath来实现不可选择的方面:)
>结果:取消选择所有选定的单元格,并在它们上调用diddeselectItemAtIndexPath:.注意:shoulddeselectItemAtIndexPath:未被调用.

预期结果:没有任何反应.

这是正常的行为吗?我在文档中找不到任何内容.如果是这样,我怎么能不去取消我的细胞呢?

解决方法

我不得不面对完全相同的问题,使用collectionView:shoulddeselectItemAtIndexPath:没有被调用.我的解决方包括手动重新选择当前选定的单元格,如果我点击一个不可选择的单元格:
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    BOOL isSelectable = /* decide if currently tapped cell should be selectable */;

    NSIndexPath *selectedItemIndexPath = /* NSIndexPath of the current selected cell in the collection view,set in collectionView:didSelectItemAtIndexPath: */;

    if (!isSelectable) {
        // the cell isn't selectable,we have to reselect the prevIoUsly selected cell that has lost selection in the meanwhile
        // without reloading first the cell the selection is not working...
        [collectionView reloadItemsAtIndexPaths:@[selectedItemIndexPath]];
        [collectionView selectItemAtIndexPath:selectedItemIndexPath animated:YES scrollPosition:UICollectionViewScrollPositionNone];
    }

    return isSelectable;
}

如果您的集合视图正在滚动(隐藏当前选定的单元格),请记住重新选择collectionView:cellForItemAtIndexPath:中的单元格.

我不太喜欢这个解决方案,它太“hacky”,但它确实有效.我希望在collectionView中执行所有逻辑:shoulddeselectItemAtIndexPath:但它没有被调用,我不明白为什么.

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...