swift – NSInternalInconsistencyException’,原因:’没有UICollectionViewLayoutAttributes实例用于-layoutAttributesForItemAtIndexPath,自定义布局

swift – NSInternalInconsistencyException’,原因:’没有UICollectionViewLayoutAttributes实例用于-layoutAttributesForItemAtIndexPath,自定义布局,第1张

概述我有一个带有自定义UICollectionLayout的UICollectionView.这一切都很好,直到我尝试插入一行… 然后我得到以下错误,似乎无法弄清楚如何解决它. *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'no UICollectionViewLay 我有一个带有自定义UICollectionLayout的UICollectionVIEw.这一切都很好,直到我尝试插入一行…

然后我得到以下错误,似乎无法弄清楚如何解决它.

*** Terminating app due to uncaught exception 'NSInternalinconsistencyException',reason: 'no UICollectionVIEwLayoutAttributes instance for -layoutAttributesForItemAtIndexPath: <NSIndexPath: 0xc000000000000016> {length = 2,path = 0 - 0}'*** First throw call stack:(0   CoreFoundation                      0x000000010915ae65 __exceptionPreprocess + 1651   libobjc.A.dylib                     0x0000000108789deb objc_exception_throw + 482   CoreFoundation                      0x000000010915acca +[NSException raise:format:arguments:] + 1063   Foundation                          0x000000010591a4de -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 1984   UIKit                               0x00000001073e361c -[UICollectionVIEwData layoutAttributesForItemAtIndexPath:] + 6275   UIKit                               0x00000001073a2859 __51-[UICollectionVIEw _vIEwAnimationsForCurrentUpdate]_block_invoke1541 + 1766   UIKit                               0x00000001073a02ed -[UICollectionVIEw _vIEwAnimationsForCurrentUpdate] + 45247   UIKit                               0x00000001073a4a5c __62-[UICollectionVIEw _updateWithItems:tentativelyForReordering:]_block_invoke1611 + 1978   UIKit                               0x0000000106be01b8 +[UIVIEw(Animation) performWithoutAnimation:] + 659   UIKit                               0x00000001073a3e07 -[UICollectionVIEw _updateWithItems:tentativelyForReordering:] + 324110  UIKit                               0x000000010739eb98 -[UICollectionVIEw _endItemAnimationsWithInvalIDationContext:tentativelyForReordering:] + 1555611  UIKit                               0x00000001073a5ee7 -[UICollectionVIEw _performBatchUpdates:completion:invalIDationContext:tentativelyForReordering:] + 41512  UIKit                               0x00000001073a5d25 -[UICollectionVIEw _performBatchUpdates:completion:invalIDationContext:] + 7413  UIKit                               0x00000001073a5cc8 -[UICollectionVIEw performBatchUpdates:completion:] + 5314  Qanda                               0x0000000103d9e0d9 _TFC5Qanda32HomeFeedCollectionVIEwController22redrawVIEwAfterNewDatafS0_FGSaCS_10Qandamodel_T_ + 410515  Qanda                               0x0000000103dad5ca _TFFFC5Qanda32HomeFeedCollectionVIEwController9fetchDataFS0_FT_T_U_FTGSqGSaCS_10Qandamodel__Sb_T_U0_FT_T_ + 127416  Qanda                               0x0000000103afc6b7 _TTRXFo__dT__XFdCb__dT__ + 3917  libdispatch.dylib                   0x0000000109dd7e5d _dispatch_call_block_and_release + 1218  libdispatch.dylib                   0x0000000109df849b _dispatch_clIEnt_callout + 819  libdispatch.dylib                   0x0000000109de02af _dispatch_main_queue_callback_4CF + 173820  CoreFoundation                      0x00000001090bad09 __CFRUNLOOP_IS_SERviciNG_THE_MAIN_disPATCH_QUEUE__ + 921  CoreFoundation                      0x000000010907c2c9 __CFRunLoopRun + 207322  CoreFoundation                      0x000000010907b828 CFRunLoopRunspecific + 48823  GraphiCSServices                    0x000000010c171ad2 GSEventRunModal + 16124  UIKit                               0x0000000106b34610 UIApplicationMain + 17125  Qanda                               0x0000000103bb537d main + 10926  libdyld.dylib                       0x0000000109e2c92d start + 1)libc++abi.dylib: terminating with uncaught exception of type NSException

这是我的布局代码:

var numberOfColumns: Int = 1let cellpadding: CGfloat = 10let buttonContainerHeight: CGfloat = 50let DEFAulT_HEIGHT: CGfloat = 80private var contentWIDth: CGfloat = UIScreen.mainScreen().bounds.wIDthprivate var totalContentHeight: CGfloat  = 0.0// Delegatevar delegate: HomeFeedCollectionVIEwLayoutDelegate?var cache = [UICollectionVIEwLayoutAttributes]()overrIDe func prepareLayout() {    // If cache is empty,calculate the layout    if cache.isEmpty {        // Set the size        let columnWIDth = contentWIDth / CGfloat(numberOfColumns)        var contentHeight: CGfloat  = 0.0        var column = 0        let xOffset: CGfloat = 0        var yOffset: CGfloat = 0        var yOffsetArray = [CGfloat]()        // First check if sections actually exist        if collectionVIEw!.numberOfSections() > 0 {            // Loop through items in section            for item in 0 ..< collectionVIEw!.numberOfItemsInSection(0) {                let indexPath = NSIndexPath(forItem: item,inSection: 0)                let wIDth = columnWIDth - cellpadding * 2                let questionHeight = delegate?.collectionVIEw(collectionVIEw!,heightForQuestionAtIndexPath: indexPath,withWIDth:wIDth)                // Calculate frame                var height: CGfloat!                if questionHeight > 0 {                    height = DEFAulT_HEIGHT + buttonContainerHeight + questionHeight! + 10                }                else {                    height = contentWIDth + buttonContainerHeight                }                if yOffsetArray.isEmpty {                    log.deBUG("yOffsetArray is EMPTY")                }                else {                    yOffset = yOffsetArray[item - 1] - cellpadding                }                yOffsetArray.append(height + yOffset)                let frame = CGRect(x: xOffset,y: yOffset,wIDth: columnWIDth,height: height)                let insetFrame = CGRectInset(frame,cellpadding,cellpadding)                // Create instance of UICollectionVIEwLayoutAttribute & set insetFrame                let attributes = UICollectionVIEwLayoutAttributes(forCellWithIndexPath: indexPath)                attributes.frame = insetFrame                cache.append(attributes)                // Make content height                contentHeight = contentHeight + (height - cellpadding)                column = column >= (numberOfColumns - 1) ? 0 : ++column            }            self.totalContentHeight = contentHeight        }    }}overrIDe func collectionVIEwContentSize() -> CGSize {    return CGSize(wIDth: UIScreen.mainScreen().bounds.wIDth,height: self.totalContentHeight + cellpadding)}overrIDe func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionVIEwLayoutAttributes]? {    var layoutAttributes = [UICollectionVIEwLayoutAttributes]()    for attributes in cache {        if CGRectIntersectsRect(attributes.frame,rect) {            layoutAttributes.append(attributes)        }    }    return layoutAttributes}

编辑:缩小错误的根源,似乎返回的UICollectionVIEwLayoutAttributes仅在首先重新加载CV时才更新为正确的新值.但是,如果我这样做,批量更新会抛出一个错误,说明AFTER的总行数应该等于之前加上插入的数字……这是有道理的.

编辑2:现在我不知所措.批量更新是否尝试从默认布局检索布局而不是我的自定义布局?

layoutAttributesForElementsInRect = [<UICollectionVIEwLayoutAttributes: 0x7ffc1d8baf70> index path: (<NSIndexPath: 0xc000000000000016> {length = 2,path = 0 - 0}); frame = (10 10; 355 405);,<UICollectionVIEwLayoutAttributes: 0x7ffc1d8a5930> index path: (<NSIndexPath: 0xc000000000200016> {length = 2,path = 0 - 1}); frame = (10 425; 355 143.222);,<UICollectionVIEwLayoutAttributes: 0x7ffc1d837240> index path: (<NSIndexPath: 0xc000000000400016> {length = 2,path = 0 - 2}); frame = (10 578.222; 355 143.222); [...] <UICollectionVIEwLayoutAttributes: 0x7ffc1d8d02f0> index path: (<NSIndexPath: 0xc000000002600016> {length = 2,path = 0 - 19}); frame = (10 3229.44; 355 143.222); ] *** Terminating app due to uncaught exception 'NSInternalinconsistencyException',reason: 'no UICollectionVIEwLayoutAttributes instance for -layoutAttributesForItemAtIndexPath: <NSIndexPath: 0xc000000000200016> {length = 2,path = 0 - 1}'

这怎么可能?

对于那些遇到同样问题的人,我发现当你这样做时
self.collectionVIEw?.performBatchUpdates({ () -> VoID in        // add new items into collection        self.collectionVIEw?.insertItemsAtIndexPaths(indexPaths)        },completion: { (finished) -> VoID in            // do insertion animations    });

不调用layoutAttributesForElementsInRect方法,而是调用layoutAttributesForItemAtIndexPat.

因此,您还必须在自定义布局中覆盖layoutAttributesForItemAtIndexPath,如下所示:

overrIDe func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionVIEwLayoutAttributes? {    // Logic that calculates the UICollectionVIEwLayoutAttributes of the item    // and returns the UICollectionVIEwLayoutAttributes    return self.singleItemLayout(indexPath)}

编辑:另外,如果您在批量insertItemsAtIndexPaths方法上遇到闪烁,那就是它自己.

总结

以上是内存溢出为你收集整理的swift – NSInternalInconsistencyException’,原因:’没有UICollectionViewLayoutAttributes实例用于-layoutAttributesForItemAtIndexPath,自定义布局全部内容,希望文章能够帮你解决swift – NSInternalInconsistencyException’,原因:’没有UICollectionViewLayoutAttributes实例用于-layoutAttributesForItemAtIndexPath,自定义布局所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1034717.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存