UICollectionView 上的断言失败
Posted
技术标签:
【中文标题】UICollectionView 上的断言失败【英文标题】:Assertion failure on UICollectionView 【发布时间】:2018-09-26 13:13:39 【问题描述】:我在我的 ios 应用程序中使用 UICollectionView,有时应用程序会因我的 collectionview 上的断言错误而崩溃。我调试它,它似乎是一个 UIKit 断言(这不是节数或单元数的问题)
有人遇到过这种错误吗?我没有找到解决它的方法。
我给你看我的代码:
var indexPaths : [IndexPath] = [IndexPath]()
for issue in issuesLoaded
let position : Int = IssueManager.sharedInstance.getPositionOfIssue(rubricId: self.rubricId!,
contentId: issue.contentId!)
let indexPath : IndexPath = IndexPath(row: position, section: 0)
indexPaths.append(indexPath)
if(position <= self.issues.count - 1)
self.issues.insert(issue, at: position)
else
self.issues.append(issue)
self.collectionView.insertItems(at: indexPaths)
self.isLoading = false
self.collectionView.collectionViewLayout.invalidateLayout()
崩溃日志是:
*** -[UICollectionView _endItemAnimationsWithInvalidationContext:tentativelyForReordering:animator:] 中的断言失败, /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3698.54.4/UICollectionView.m:5867
还有stackTrace:
> Thread 1 Queue : com.apple.main-thread (serial)
> #0 0x00000001833a92e0 in __pthread_kill ()
> #1 0x000000018354a288 in pthread_kill$VARIANT$mp ()
> #2 0x0000000183317d0c in abort ()
> #3 0x0000000103c68b0c in uncaught_exception_handler ()
> #4 0x00000001839231c8 in __handleUncaughtException ()
> #5 0x0000000182adc8c8 in _objc_terminate() ()
> #6 0x0000000182acd37c in std::__terminate(void (*)()) ()
> #7 0x0000000182accccc in __cxa_throw ()
> #8 0x0000000182adc720 in objc_exception_throw ()
> #9 0x0000000183922bf8 in +[NSException raise:format:arguments:] ()
> #10 0x0000000184312fa0 in -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] ()
> #11 0x000000018d67b240 in -[UICollectionView _endItemAnimationsWithInvalidationContext:tentativelyForReordering:animator:]
> ()
> #12 0x000000018d679844 in -[UICollectionView _updateRowsAtIndexPaths:updateAction:updates:] ()
> #13 0x000000018d679434 in -[UICollectionView insertItemsAtIndexPaths:] ()
> #14 0x00000001027a1f8c in closure #1 in ContentViewCell.loadNextIssues() at
> /Users/username/Project/Views/ContentViewCell.swift:167
> #15 0x00000001029ddc3c in closure #1 in closure #1 in closure #2 in closure #1 in closure #1 in closure #1 in
> IssueManager.getIssueForContentIds(contentIds:completionHandler:) at
> /Users/username/Project/Manager/IssueManager.swift:684
> #16 0x0000000102787b68 in thunk for @escaping @callee_guaranteed () -> () ()
> #17 0x00000001049c11dc in _dispatch_call_block_and_release ()
> #18 0x00000001049c119c in _dispatch_client_callout ()
> #19 0x00000001049c5d2c in _dispatch_main_queue_callback_4CF ()
> #20 0x00000001838cb070 in __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ ()
> #21 0x00000001838c8bc8 in __CFRunLoopRun ()
> #22 0x00000001837e8da8 in CFRunLoopRunSpecific ()
> #23 0x00000001857ce020 in GSEventRunModal ()
> #24 0x000000018d808758 in UIApplicationMain ()
> #25 0x000000010299d1a4 in main at /Users/username/Project/AppDelegate.swift:15
感谢您的帮助!!
【问题讨论】:
是不是这段代码调用太频繁太快,导致之前的self.collectionView.insertItems(at: indexPaths)
还没调用完就又被调用了?
是的,我使用了保护措施来避免这个问题,但问题仍然存在
您尝试过批量更新块吗? func performBatchUpdates(_ updates: (() -> Void)?, completion: ((Bool) -> Void)? = nil)
有了它,您可以一次执行多个插入、删除、重新加载或移动操作,而不必单独执行!
【参考方案1】:
尝试在每次插入后重新加载 collectionView
【讨论】:
是的,但我想避免重新加载集合以避免视图闪烁 您是否尝试过重新加载特定的索引路径?试试这个.. collectionView.reloadItemsAtIndexPaths(myArrayOfIndexPaths)以上是关于UICollectionView 上的断言失败的主要内容,如果未能解决你的问题,请参考以下文章
UICollectionView 断言失败 -[UICollectionView _updateWithItems:tentativelyForReordering:]
deleteItemsAtIndexPaths 中的 UICollectionView 断言失败
UICollectionView + iOS 7 / Xcode 5 = 断言失败
带有 endItemAnimations 的 UICollectionView 中的断言失败
createPreparedCellForItemAtIndexPath 中的 UICollectionView 断言失败