Ios UICollectionView中的断言失败?
我在我的Ios UICollectionView中的断言失败?,ios,xcode,swift,debugging,uicollectionview,Ios,Xcode,Swift,Debugging,Uicollectionview,我在我的UICollectionViewCells中有一些在线托管的图像被解析为UIImageViews。当我进出视图时,我经常在调试器中遇到崩溃和以下错误: *** Assertion failure in -[UICollectionView _endItemAnimationsWithInvalidationContext:tentativelyForReordering:], /SourceCache/UIKit/UIKit-3347.44.2/UICollectionView.m:38
UICollectionViewCell
s中有一些在线托管的图像被解析为UIImageView
s。当我进出视图时,我经常在调试器中遇到崩溃和以下错误:
*** Assertion failure in -[UICollectionView _endItemAnimationsWithInvalidationContext:tentativelyForReordering:], /SourceCache/UIKit/UIKit-3347.44.2/UICollectionView.m:3870
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to delete item 0 from section 0 which only contains 0 items before the update'
*** First throw call stack:
(0x1839702d8 0x19563c0e4 0x183970198 0x184824ed4 0x188a74348 0x100150300 0x100117c4c 0x100117d88 0x18332f010 0x1848571c4 0x1847a8604 0x1847981cc 0x184859f28 0x101410f94 0x101415c28 0x1839277f8 0x1839258a0 0x1838512d4 0x18d2a76fc 0x18844ef40 0x10011a1d4 0x195ce6a08)
这个错误的原因是什么?我应该如何消除它
更新
我观察到,当我切换到采集视图
,并在任何单元加载之前快速返回时,会发生崩溃。。但它不应该因为这个原因而崩溃。我该如何解决这个问题
这是我的手机号码。。方法
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! MyCollectionViewCell
// Configure the cell
let book = self.books[indexPath.row]
let coverImage = book.coverImage
if coverImage == nil {
book.fetchCoverImage({ (image, error) -> Void in
collectionView.reloadItemsAtIndexPaths([indexPath])
})
} else {
let imageView = cell.imageView
imageView.image = book.coverImage
}
return cell
}
尝试断开重新加载项的块
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! MyCollectionViewCell
// Configure the cell
let book = self.books[indexPath.row]
let coverImage = book.coverImage
if coverImage == nil {
book.fetchCoverImage({ (image, error) -> Void in
if collectionView != nil { // <--- something like this. I'm not sure. Just take the idea: try to check before reload.
collectionView.reloadItemsAtIndexPaths([indexPath])
}
})
} else {
let imageView = cell.imageView
imageView.image = book.coverImage
}
return cell
}
override func collectionView(collectionView:UICollectionView,cellForItemAtIndexPath indexPath:NSIndexPath)->UICollectionViewCell{
让cell=collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier,forIndexPath:indexPath)作为!MyCollectionViewCell
//配置单元格
让book=self.books[indexPath.row]
让coverImage=book.coverImage
如果coverImage==nil{
book.fetchCoverImage({(图像,错误)->中的Void
如果collectionView!=无{//添加一个异常断点并查看调用堆栈以确定问题所在-显示您的代码我尝试过,但这次不会崩溃。我问题中的错误实际上是抛出的一个NSException。当我在加载任何内容之前突然退出集合视图屏幕时,就会发生这种情况。好吧,这就是我所看到的served@Paulw11Show more your code please.cellAtIndex,…我认为问题在于这个块{(图像,错误)->collectionView中的Void.reloadItemsAtIndexPaths([indexPath])退出集合时将调用
。是否可以尝试检查collectionView!=nil
,或者在退出时使用标志或其他方法来打破此块?能否在回答中提供该代码?