ios - 如果 cellForItemAtIndexPath : is called in didSelectItemAtIndexPath: 有什么问题

标签 ios uicollectionview uicollectionviewcell uicollectionviewdelegate

我在 didSelectItemAtIndexPath: 方法中调用了 cellForItemAtIndexPath:。结果是 didSelectItemAtIndexPath: 每个单元格只被调用一次。所以我可以第一次点击一个单元格,然后我可以点击另一个单元格,但是如果我再次点击第一个单元格,则不再调用 didSelectItemAtIndexPath (但所有其他"new"单元格) .

现在我不再调用 cellForItemAtIndexPath:,现在 didSelectItemAtIndexPath: 按预期工作。但是为什么不允许我这样做呢?有什么原因吗?

最佳答案

Collection View 为您缓存单元格的实例,当您在索引路径上创建单元格时,您将它们出列。 Collection View 对执行此操作时发生的情况做出一些假设,因此如果您开始对同一索引路径中的单元格发出多个请求,奇怪的事情就会开始发生。有时这会导致视觉问题,有时会导致交互问题。

如果您想获取指定索引路径处的单元格,您应该直接询问 Collection View 而不是使用您自己的委托(delegate)方法,这将为您提供屏幕上的现有单元格而不是新单元格。

关于ios - 如果 cellForItemAtIndexPath : is called in didSelectItemAtIndexPath: 有什么问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35335220/

相关文章:

ios - 如何通过拖放创建新的 UIWindowScene?

ios - iOS CoreBluetooth被动检查蓝牙是否已启用,而不提示用户打开蓝牙

ios - YTPlayerView ipad全屏播放视频

ios - UICollectionView 将单元​​格包裹在标签周围

ios - 从collectionView中获取indexpath,有时会获取错误的indexPath

ios - .o 文件中缺少汇编函数

ios - UICollectionView 重新加载数据问题

ios - swift 3 : Resizing UICollectionViewCell programmatically causes cells to overlap

ios - 在 Parse iOS 中为每个对象抓取第一张图像

swift - UICollectionViewCell 未正确显示