- XCode: Version 16.2 (16C5032a)
- iOS 18.3.1
-
UITableview
和UICollectionView
在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
问题解决!!!