什么时候
collectionView.allowsMultipleSelection = YES;
我可以取消选择已选择的单元格。
当
collectionView.allowsMultipleSelection = NO;
我无法取消选择已选择的单元格。
有没有反正我只能设置
collectionView.allowsMultipleSelection = NO;
和
可以取消选中的单元格吗?所以要么有一个被选中,要么没有被选中。
我知道您可以通过点击手势实现自己的选择,然后在检测到手势时调用 setSelected。但我正在寻找一种更原生的解决方案,您可以在 uicollectionView 本身上进行配置。
谢谢!
最佳答案
我遇到了同样的问题,但找不到本地解决方案。这就是我最终这样做的方式,有点老套,但它可以满足需要。我在 viewDidLoad
中设置了 self.collectionView.allowsMultipleSelection = YES
。
- (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
中的额外选择和取消选择是为了动画取消选择 - 这种方法提供的额外好处是能够动画转换。
关于ios - UICollectionView 在禁用 allowsMultipleSelection 时启用取消选择单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32423778/