iOS18.0+ UICollectionView崩溃问题

  • XCode: Version 16.2 (16C5032a)
  • iOS 18.3.1
  • UITableviewUICollectionView 在iOS18.0之前并不会发生崩溃,但是在18.0+ 的版本,发生崩溃。

报错问题:

Thread 1: "Expected dequeued view to be returned to the collection view in preparation for display. When the collection view's data source is asked to provide a view for a given index path, ensure that a single view is dequeued and returned to the collection view. Avoid dequeuing views without a request from the collection view. For retrieving an existing view in the collection view, use -[UICollectionView cellForItemAtIndexPath:] or -[UICollectionView supplementaryViewForElementKind:atIndexPath:]

这个大概意思,通过dequeue通过index检索去获取cell时,应当使用UICollectionView cellForItemAtIndexPath。或-[UICollectionView supplementaryViewForElementKind:atIndexPath:]。避免在没有来自集合视图的请求的情况下对视图进行脱队。

在我的代码里,想通过index获取一个cell实例。

collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: index) as? DemoCCell

以上的代码其实应当在func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell里的,我在其他地方使用了。这才是崩溃的原因

修改:通过建议的方式来通过index获取cell的实例。

collectionView.cellForItem(at: index) as? DemoCCell

问题解决!!!

参考文献:Crash in iOS18

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容