layoutAttributesForItemAtIndexPath崩溃,索引超出范围

时间:2016-11-17 10:14:00

标签: uicollectionview uicollectionviewlayout

我在我的应用中遇到了崩溃问题,我已经实现了自定义布局集合视图以在iPhone中显示书页。当我重新排序页面时(例如 - 如果我想将书中的最后一个放到第一位)应用程序有时会在我的自定义布局代码(layoutAttributesForItemAtIndexPath)中崩溃。深入研究代码,发现API在indexPath中接收超出范围。行值太大而无法转换为-1,因为我的布局属性数组中此位置没有数据,应用程序崩溃。这发生在iOS 9.x而不是10.x.

有谁知道我为什么会遇到此次崩溃。

PS:附加了Stacktrace。

enter image description here

1 个答案:

答案 0 :(得分:0)

您必须实施layoutAttributesForItemAtIndexPath()

override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
    return cache[indexPath.item]
}

I found this solution here and it solved my problem