具有多选的UICollectionView不会选择十几个项目 [英] UICollectionView with multi-select won't select more than a dozen of items

查看:179
本文介绍了具有多选的UICollectionView不会选择十几个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用 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似乎非常随机。您可以选择一些项目,向下滚动,再选择一些项目,在某些时候您无法再选择任何项目。

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.

推荐答案

我发现虽然我的以前的答案有效,可能是因为没有打电话给超级。虽然 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 的错误。

正在发生的事情是先前选择的单元格正在被重用并保持选定的状态。集合视图未将选中设置为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 之后选择为YES。

If the reused cell is selected, the collection view will set selected to "YES" after prepareForReuse is called.

这是某事 UICollectionView 应该自己做。谢天谢地,解决方案很简单。不幸的是,通过跟踪我自己的选择状态,我花了很多时间来解决这个bug。我没有意识到为什么它正在发生,直到我正在处理另一个具有较小单元格的项目。

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屋!

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