UICollectionView cellForItem 调用不正确 [英] UICollectionView cellForItem called not correctly

查看:31
本文介绍了UICollectionView cellForItem 调用不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Xcode 8.2.1将 Swift 2.2 迁移到 Swift 3 之后

UICollectionView 的 cellForItem 调用不正确,但在迁移之前工作正常

我有带有水平 UICollectionViewFlowLayout 的 CollectionView并设置分页集为真

UIViewController 的视图框架与 UICollectionView 的框架相同,也是单元格的框架,如页面应用程序(全尺寸)

当我向右滚动 UICollectionView 时,我在 cellForItem(UICollectionViewDataSource func) 中打印 indexPath.row

indexPath.row 打印如下

0、3、4、5、6、7、8、9、10

然后滚动到上次打印时的相反方向10

下一个 indexPath 是 7 但它应该是 9 因为最后调用的索引是 10

滚动直到第一个单元格显示

6、5、4、3、2、1、0打印

为什么会出现这种情况?

在迁移之前的 Swift 2.2 中,工作正常

在 Swift 3 或 iOS 10 之后是否会更改为 UICollectionView 或 UICollectionViewFlowLayout?

解决方案

从 Swift 2.2 迁移到 Swift 3 之后

iOS10中的UICollectionView预取设置为true(默认)

所以 cellForItem 没有被称为线性序列

我在这篇文章中得到了提示

Xcode 8.2.1 after migrate Swift 2.2 to Swift 3

UICollectionView's cellForItem not called correctly but before migration was working fine

I have CollectionView with Horizontal UICollectionViewFlowLayout and set paging set true

UIViewController's view frame is same to UICollectionView's frame also cell's frame like page application (full size)

I print indexPath.row in cellForItem(UICollectionViewDataSource func) when I scroll UICollectionView to right

indexPath.row printed following

0, 3, 4, 5, 6, 7, 8, 9, 10

then scroll to opposite direction when last printed 10

next indexPath is 7 but it should be 9 because last called index is 10

and scrolling until first cell showing

6, 5, 4, 3, 2, 1, 0 printed

why this situation happening?

In Swift 2.2 before migrate, work fine

is there a changing to UICollectionView or UICollectionViewFlowLayout after Swift 3 or iOS 10?

解决方案

After migrate to Swift 3 from Swift 2.2

UICollectionView In iOS10 prefetch set true (by default)

so cellForItem was not called as linear sequence

I get a hint in this post little bites of cocoa

prefetching is for datasource instead for cell

so somethimes not calling cellForItem(this is for cell) linearly (what i want)

just uncheck prefetching enable it works fine like before migration

这篇关于UICollectionView cellForItem 调用不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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