带有多选的 UICollectionView 不会选择十几个项目 [英] UICollectionView with multi-select won't select more than a dozen of items
问题描述
当使用 UICollectionView
并将 allowsMultipleSelection
设置为 YES 时,只有十几个项目是可选的.UICollectionViewDelegate
停止调用 collectionView:didSelectItemAtIndexPath:
.
When using a UICollectionView
with allowsMultipleSelection
set to YES only a dozen items are selectable. UICollectionViewDelegate
stops calling collectionView:didSelectItemAtIndexPath:
.
看起来很随意.您可以选择一些项目、向下滚动、选择更多项目,但在某些时候您无法再选择任何项目.
It seems very random. You can select a few items, scroll down, select some more, and at some point you're not able to select any more items.
当单元格较小时,您似乎可以选择更多项目.单元格越大,在它停止工作之前您可以选择的项目就越少.
When the cell is smaller you seem to be able to select more items. The larger the cell, the fewer items you're able to select before it stops working.
推荐答案
我发现虽然我之前的答案有效,但可能是由于没有调用 super.虽然 UICollectionReusableView
的文档没有提到这一点,但具有相同方法的 UITableViewCell
的文档却提到了这一点.
I have discovered that while my previous answer works, it may be caused by not calling super. While the documentation for UICollectionReusableView
fails to mention this, the documentation for UITableViewCell
, which has the same method, does.
- (void)prepareForReuse
{
[super prepareForReuse]
// Your code here.
}
旧答案:
这可能是 UICollectionView
的错误.
发生的情况是先前选择的单元格正在被重复使用并保持选定状态.集合视图未将 selected
设置为NO".
What's happening is cells that were previously selected are being reused and maintain the selected state. The collection view isn't setting selected
to "NO".
解决方法是在单元格的prepareForReuse
中重置选中状态:
The solution is to reset the the selected state in prepareForReuse
of the cell:
- (void)prepareForReuse
{
self.selected = NO;
}
如果重用单元格被选中,集合视图将在调用prepareForReuse
后将selected
设置为YES".
If the reused cell is selected, the collection view will set selected
to "YES" after prepareForReuse
is called.
这是 UICollectionView
应该自己做的事情.幸运的是,解决方案很简单.不幸的是,我花了很多时间通过跟踪我自己的选择状态来解决这个错误.我没有意识到为什么会发生这种情况,直到我正在从事另一个具有较小单元的项目.
This is something the UICollectionView
should be doing on it's own. Thankfully the solution is simple. Unfortunately I spent a ton of time working around this bug by tracking my own select state. I didn't realize why it was happening until I was working on another project with smaller cells.
这篇关于带有多选的 UICollectionView 不会选择十几个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!