UICollectionView 启用取消选择单元格而allowsMultipleSelection 被禁用 [英] UICollectionView Enable deselecting cells while allowsMultipleSelection is disabled

查看:20
本文介绍了UICollectionView 启用取消选择单元格而allowsMultipleSelection 被禁用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么时候

collectionView.allowsMultipleSelection = YES;

我可以取消选择已选择的单元格.

I can deselect cells that were selected.

什么时候

collectionView.allowsMultipleSelection = NO;

我无法取消选择已选择的单元格.

I cannot deselect cells that were selected.

反正我只能设置

collectionView.allowsMultipleSelection = NO;

能够取消选择选定的单元格吗?所以要么选择一个要么没有选择.

be able to deselect the selected cell? so there would either be one selected or none selected.

我知道您可以通过点击手势实现自己的选择,然后在检测到手势时调用 setSelected.但我正在寻找一种更原生的解决方案,您可以在 uicollectionView 本身上进行配置.

I understand you can implement your own selection with a tap gesture then calling setSelected when gesture is detected. But I am looking for a more native solution, something that you could configure on uicollectionView itself.

谢谢!

推荐答案

我遇到了同样的问题,找不到本地解决方案.这就是我最终这样做的方式,有点hacky,但它完成了所需的工作.我在 viewDidLoad 中设置了 self.collectionView.allowsMultipleSelection = YES.

I had the same problem and couldn't find a native solution. This is how I ended up doing it, a bit hacky but it does what's needed. I have self.collectionView.allowsMultipleSelection = YES set in viewDidLoad.

- (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 中额外的选择和取消选择是为了动画取消选择 - 这种方法提供的另一个好处,能够动画过渡.

The additional selection and deselection in didDeselectItemAtIndexPath is to animate the deselection - an additional benefit this approach provides, being able to animate the transitions.

这篇关于UICollectionView 启用取消选择单元格而allowsMultipleSelection 被禁用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆