xcode UICollectionView 中的断言失败?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32364693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Assertion failure in UICollectionView?
提问by Mike Strong
I have some images hosted online that are being parsed into UIImageView
s in my UICollectionViewCell
s. When I enter in and out of the view, I frequently get a crash and the following error in the debugger :
我有一些在线托管的图像UIImageView
在我的UICollectionViewCell
s中被解析为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)
What is the cause of this error and how should I get rid of it?
这个错误的原因是什么,我应该如何摆脱它?
UPDATE
更新
I observed that the crash occurs when i segue to the collectionView
, and quickly return back before any of the cells load.. but it shouldn't crash because of this reason. How do I fix this?
我观察到崩溃发生在我继续collectionView
, 并在任何单元加载之前快速返回..但由于这个原因它不应该崩溃。我该如何解决?
here is my cellForIndex.. Method
这是我的 cellForIndex .. 方法
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
}
采纳答案by tuledev
Try to break block of reloadItems
尝试打破 reloadItems 块
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
}