UICollectionView 选择和取消选择问题 [英] UICollectionView Select and Deselect issue
问题描述
所以我有一个主对象,其中有许多与之关联的图像.Image 也是一个对象.
So I have a main object that has many images associated with it. An Image is also an object.
假设你有一个集合视图控制器,并且在那个控制器中你有
Say you have a collection view controller, and in that controller you have
cellForItemAtIndexPath
基于主要对象,如果它具有与之关联的当前图像,我想将selected设置为true.但我希望用户能够随时取消选择"当前单元格以删除其与主对象的关联.
well based on the main object, if it has the current image associated with it I want to set selected to true. But I want the user to be able to "un-select" that current cell at any time to remove its association with the main object.
我发现如果您将selected 设置为 true" - 如果 cellForItemAtIndexPath
中的主要对象和图像之间存在关系,则不再可以选择取消选择.
I find that if you set "selected to true" - if there is an relation between the main object and image in cellForItemAtIndexPath
, de-selection is no longer an option.
在
didDeselectItemAtIndexPath
和
didSelectItemAtIndexPath
我使用日志进行测试以查看它们是否被调用.如果一个单元格设置为选中 - 下界会被调用,但如果我从未在 cellForItemAtIndexPath
中将一个单元格设置为选中,我可以选择和取消选择所有我想要的.
I test with a log to see if they are called. If a cell is set to selected - nether gets called, but If I never set a cell to selected in cellForItemAtIndexPath
I can select and deselect all I want.
这是集合视图应该工作的预期方式吗?我阅读了文档,但似乎并没有谈到这一点.我将文档解释为它以表格视图单元格的方式工作.有一些明显的变化
Is this the intended way a collection view is supposed to work? I read the docs and it does not seem to talk about this being so. I interpret the docs to mean it works the way a table view cell would. with a few obvious changes
这也表明控制器设置正确并且正在使用适当的委托方法....嗯
This also shows the controller is set up correct and is using the appropriate delegate methods.... hmmmm
推荐答案
我有同样的问题,即.在 [UICollectionView collectionView:cellForItemAtIndexPath:]
中设置 cell.selected = YES
然后无法通过点击取消选择单元格.
I had the same issue, ie. setting cell.selected = YES
in [UICollectionView collectionView:cellForItemAtIndexPath:]
then not being able to deselect the cell by tapping on it.
目前的解决方案:我在 中调用 both
.[UICollectionViewCell setSelected:]
和 [UICollectionView selectItemAtIndexPath:animated:scrollPosition:]
[UICollectionView collectionView:cellForItemAtIndexPath:]
Solution for now: I call both [UICollectionViewCell setSelected:]
and [UICollectionView selectItemAtIndexPath:animated:scrollPosition:]
in [UICollectionView collectionView:cellForItemAtIndexPath:]
.
这篇关于UICollectionView 选择和取消选择问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!