如何将取消选择添加到 UICollectionView 但没有多选? [英] How to add deselecting to UICollectionView but without multiple selection?

查看:21
本文介绍了如何将取消选择添加到 UICollectionView 但没有多选?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在一个视图上有 4 个不同的集合视图,我想允许取消选择.为此,我必须设置:

I currently have 4 different collection views on a view, and I want to allow deselection. For this I have to set:

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

但是我只想能够从每个集合视图中选择一个单元格.例如,在 bagsCollectionView 中,用户可以选择一个包,但也可以取消选择并选择另一个包.他们不能挑两个包或两双鞋.其他三个集合视图也一样!

However I only want to be able to select one cell from each collectionview. E.g., in bagsCollectionView the user can choose one bag but can also deselect that and pick another bag. They cannot pick two bags or two shoes. Same for the other three collection views!

我该怎么做?

推荐答案

allowsMultipleSelection 被禁用时,没有内置支持取消选择选定的单元格(通过再次点击它).但这既是有意的,也是直觉的.默认行为并不意味着对可以打开然后关闭的开关进行建模.

There's no built-in support to deselect a selected cell (by tapping it again) when allowsMultipleSelection is disabled. But this is both intentional and intuitive. The default behavior is not meant to model a switch that you can toggle on, then toggle off.

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

When you consider a single-selection design where an item (or table row) is selected, when a user wants to select a different item (or table row), the intuitive behavior is to tap another item (at which point the original item is automatically deselected by the system). They don't (think about needing to) tap the original item to deselect it before selecting the other item.

您可能正在尝试对一些通常不符合预期的内容进行建模,并且通过呈现与其他应用程序不同的交互,从 UX 的角度来看,这会有点令人迷惑.用户可能不会有意识地知道出了什么问题,但它可能会影响整个应用程序体验.这可能代表设计问题(以及涉及一些不必要的代码).

You may be trying to model something that isn't generally expected, and by presenting a different interaction from other apps, it would be slightly disorienting from a UX point of view. The user may not consciously know what's wrong, but it may tinge the whole app experience. That may represent a design problem (as well as involve some unnecessary code).

如何处理您的需求

要取消选择但强制执行单选,您需要启用多项选择,然后在进行新选择时强制取消选择其他单元格.

To implement deselection but enforce single-selection, you'll need to enable multiple selection, then enforce deselection of other cells when a new selection is made.

- (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
}

这篇关于如何将取消选择添加到 UICollectionView 但没有多选?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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