UICollectionView启用取消选中单元格,同时禁用允许MultipleSelection

什么时候

collectionView.allowsMultipleSelection = YES; 

我可以取消select的单元格。

什么时候

 collectionView.allowsMultipleSelection = NO; 

我无法取消选中的单元格。

无论如何,我只能设置

 collectionView.allowsMultipleSelection = NO; 

能否取消选定的单元格? 所以要么select一个,要么没有select。

我知道你可以实现你自己的select,然后在检测到手势时调用setSelected。 但我正在寻找一个更原生的解决scheme,你可以在uicollectionView自己configuration的东西。

谢谢!

我有同样的问题,找不到本机解决scheme。 这就是我最终做到的,有点冒险,但是它确实需要。 我有self.collectionView.allowsMultipleSelection = YESviewDidLoad设置。

 - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { for (NSIndexPath *selectedIndexPath in [self.collectionView indexPathsForSelectedItems]) { [self.collectionView deselectItemAtIndexPath:selectedIndexPath animated:NO]; } [collectionView selectItemAtIndexPath:indexPath animated:YES scrollPosition:UICollectionViewScrollPositionNone]; } - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath { [collectionView selectItemAtIndexPath:indexPath animated:YES scrollPosition:UICollectionViewScrollPositionNone]; [collectionView deselectItemAtIndexPath:indexPath animated:YES]; } 

didDeselectItemAtIndexPath的额外select和取消select是为取消select设置animation效果 – 这种方法提供了额外的好处,可以为animation制作animation。