UICollectionView shouldSelectItemAtIndexPath=NO 不会避免取消选择旧选择? [英] UICollectionView shouldSelectItemAtIndexPath=NO does not avoid deselecting old selection?

查看:24
本文介绍了UICollectionView shouldSelectItemAtIndexPath=NO 不会避免取消选择旧选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 CollectionView 中有多个项目,但只有其中几个是可选的.我正在使用委托方法处理此问题:

I have multiple items in a CollectionView, but only a few of them should be selectable. I'm handling this with the delegate method:

- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath

如果选择了一个可选项目,并且在下一步中,shouldSelectItemAtIndexPath 拒绝了一个不可选项目的选择,返回 NO,则所选项目无论如何都会被取消选择.

My problem occurs if a selectable item is selected and in the next step the selection of a not selectable item is rejected by shouldSelectItemAtIndexPath returning NO, the selected item gets deselected anyway.

我也试过使用

- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath

但这是同样的问题.

这是 UICollectionView 正确的期望行为吗?如果是,如果选择了不可选择的项目,我如何避免取消选择上次选择的项目?

Is this the correct desired behaviour of UICollectionView? And if yes, how can i avoid the deselection of my last selected item if a not selectable item gets selected?

推荐答案

我必须纠正我的假设:最后一个选择不会被取消选择!

I have to correct my assumption: The last selection won't get deselected!

为了在选择时更改单元格的外观,我覆盖了 UICollectionViewCell 的 setSelected 访问器.选择非可选项目时,将多次调用上次选定的单元格中的访问器设置.首先状态为 NO,然后状态为 YES,最后再次为 NO.最后一个状态 NO 导致我的单元格将其外观设置为非选定单元格的外观.

In order to change the appearance of the cell on selection I have overridden the setSelected accessor of UICollectionViewCell. When selecting a non selectable item, the accessor setSelected of the last selected cell gets called multiple times. First with the state NO, then with the state YES and in the end with NO again. The last state NO caused my cell to set its appearance to that of an non selected cell.

我不知道这种奇怪行为的原因,也无法解决.

I don't know the reason for this weird behavior nor could i solve it.

我的解决方法是直接在 ViewController 中更改所选单元格的外观.

My workaround is to change the appearance of selected cells directly in the ViewController.

设置选中的外观:

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath

移除选中的外观:

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath

检查当前单元格是否被选中并按预期更改外观.

   - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

这篇关于UICollectionView shouldSelectItemAtIndexPath=NO 不会避免取消选择旧选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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