如何添加取消select到UICollectionView但没有多个select?

我目前有一个视图有4个不同的采集视图,我想允许取消select。 为此,我必须设置:

//Stop multiple selections on collectionview self.bagsCollectionView.allowsMultipleSelection = YES; self.shoesCollectionView.allowsMultipleSelection = YES; self.dressesCollectionView.allowsMultipleSelection = YES; self.jewelleryCollectionView.allowsMultipleSelection = YES; 

不过,我只想从每个collectionview中select一个单元格。 例如,在bagsCollectionView中,用户可以select一个包,但也可以取消select另一个包。 他们不能挑两个袋子或两个鞋子。 其他三个收集意见也一样!

我该怎么做呢?

当禁用allowsMultipleSelection时,没有内置支持来取消select所选单元格(通过再次点击)。 但这是有意和直观的。 默认行为并不意味着对可以切换的开关build模,然后closures。

当你考虑select一个项目(或表格行)的单选devise时,当用户想要select不同的项目(或表格行)时,直观的行为是点击另一个项目(此时原始项目是系统自动取消select)。 在select其他项目之前,他们不(需要考虑)点击原始项目以取消select它。

您可能试图对通常不期望的事物进行build模,并通过展示与其他应用程序的不同交互,从UX观点来看,这会略微迷失方向。 用户可能不会有意识地知道什么是错的,但是它可能会使整个应用程序的体验变得微不足道。 这可能代表一个devise问题(以及涉及一些不必要的代码)。

如何处理您的要求

要实现取消select,但强制单选,您需要启用多项select,然后在执行新select时强制取消select其他单元格。

 - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { NSArray *indexPaths = collectionView.indexPathsForSelectedItems; // Don't deselect our own index path [indexPaths removeObject:indexPath]; for (NSIndexPath *otherIndexPath in indexPaths) { [collectionView deselectItemAtIndexPath:otherIndexPath animated:YES]; } // ... Do whatever else you need to do in response to the selection }