【发布时间】:2023-04-06 11:43:01
【问题描述】:
在viewDidLoad 上,我为UICollectionViewCell 设置了collectionViewLayout,UILabel 表示“正在加载内容...”,当应用程序从服务器获取内容时,它会执行以下操作:
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
...
// some configuration for the layout
// here it crash:
collectionView.setCollectionViewLayout(layout, animated: true)
说这个消息会崩溃:
* 由于未捕获的异常“NSRangeException”而终止应用程序,原因:“* -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array” *** 首先抛出调用堆栈: (0x24479b0b 0x23c36dff 0x2438a6d3 0x29275999 0x29271d13 0x29272aaf 0x28be537f 0x28be4667 0x1778dc 0x165f48 0x1661b8 0x16e774 0x16f038 0xf950c 0xf9174 0xdefb0 0xe1934 0xdf98c 0x52ed78 0x52d700 0x52bfc8 0x4df9c0 0x1531ba7 0x1531b93 0x1536659 0x2443b755 0x24439c4f 0x243881c9 0x24387fbd 0x259a4af9 0x28ac0435 0x131254 0x24034873) libc++abi.dylib:以 NSException 类型的未捕获异常终止
我所做的事情是设置一个collectionViewLayout 用另一个替换它。
我试图通过 invalidateLayout() 重新加载或不重新加载 collectionView 来替换 collectionViewLayout 之前使布局无效,但它仍然崩溃。
如何动态更改collectionViewLayout?或者我做错了什么?
谢谢
【问题讨论】:
-
尝试添加异常断点,它可能有助于查明您的问题 - developer.apple.com/library/ios/recipes/…
-
至于问题本身 - 在您访问数组时检查代码中的位置并确保其中确实存在某些内容。例外说明了一切。
-
@Losiowaty 我认为数组不是问题,因为如果我不尝试替换
collectionViewLayout它可以完美运行 -
@Losiowaty 我添加了一个异常断点,它在
collectionView.setCollectionViewLayout(layout, animated: true)停止了 -
@Losiowaty 我找到了实现
UICollectionViewDelegateFlowLayout的解决方案,我想我将使用该委托来替换整个布局对象。
标签: ios swift uicollectionview uicollectionviewlayout