NSInternalInconsistencyException',原因:'-layoutAttributesForItemAtIndexPath 没有 UICollectionViewLayoutAttributes 实例,自定义布局 [英] NSInternalInconsistencyException', reason: 'no UICollectionViewLayoutAttributes instance for -layoutAttributesForItemAtIndexPath, custom layout

查看:91
本文介绍了NSInternalInconsistencyException',原因:'-layoutAttributesForItemAtIndexPath 没有 UICollectionViewLayoutAttributes 实例,自定义布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有自定义 UICollectionLayout 的 UICollectionView.一切正常,直到我尝试插入一行...

I have a UICollectionView with a custom UICollectionLayout. It all works fine, until I try to insert a row…

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

Then I get the following error and can't seem to figure out how to solve it.

*** 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 + 165
1   libobjc.A.dylib                     0x0000000108789deb objc_exception_throw + 48
2   CoreFoundation                      0x000000010915acca +[NSException raise:format:arguments:] + 106
3   Foundation                          0x000000010591a4de -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 198
4   UIKit                               0x00000001073e361c -[UICollectionViewData layoutAttributesForItemAtIndexPath:] + 627
5   UIKit                               0x00000001073a2859 __51-[UICollectionView _viewAnimationsForCurrentUpdate]_block_invoke1541 + 176
6   UIKit                               0x00000001073a02ed -[UICollectionView _viewAnimationsForCurrentUpdate] + 4524
7   UIKit                               0x00000001073a4a5c __62-[UICollectionView _updateWithItems:tentativelyForReordering:]_block_invoke1611 + 197
8   UIKit                               0x0000000106be01b8 +[UIView(Animation) performWithoutAnimation:] + 65
9   UIKit                               0x00000001073a3e07 -[UICollectionView _updateWithItems:tentativelyForReordering:] + 3241
10  UIKit                               0x000000010739eb98 -[UICollectionView _endItemAnimationsWithInvalidationContext:tentativelyForReordering:] + 15556
11  UIKit                               0x00000001073a5ee7 -[UICollectionView _performBatchUpdates:completion:invalidationContext:tentativelyForReordering:] + 415
12  UIKit                               0x00000001073a5d25 -[UICollectionView _performBatchUpdates:completion:invalidationContext:] + 74
13  UIKit                               0x00000001073a5cc8 -[UICollectionView performBatchUpdates:completion:] + 53
14  Qanda                               0x0000000103d9e0d9 _TFC5Qanda32HomeFeedCollectionViewController22redrawViewAfterNewDatafS0_FGSaCS_10QandaModel_T_ + 4105
15  Qanda                               0x0000000103dad5ca _TFFFC5Qanda32HomeFeedCollectionViewController9fetchDataFS0_FT_T_U_FTGSqGSaCS_10QandaModel__Sb_T_U0_FT_T_ + 1274
16  Qanda                               0x0000000103afc6b7 _TTRXFo__dT__XFdCb__dT__ + 39
17  libdispatch.dylib                   0x0000000109dd7e5d _dispatch_call_block_and_release + 12
18  libdispatch.dylib                   0x0000000109df849b _dispatch_client_callout + 8
19  libdispatch.dylib                   0x0000000109de02af _dispatch_main_queue_callback_4CF + 1738
20  CoreFoundation                      0x00000001090bad09 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
21  CoreFoundation                      0x000000010907c2c9 __CFRunLoopRun + 2073
22  CoreFoundation                      0x000000010907b828 CFRunLoopRunSpecific + 488
23  GraphicsServices                    0x000000010c171ad2 GSEventRunModal + 161
24  UIKit                               0x0000000106b34610 UIApplicationMain + 171
25  Qanda                               0x0000000103bb537d main + 109
26  libdyld.dylib                       0x0000000109e2c92d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

这是我的布局代码:

var numberOfColumns: Int = 1
let cellPadding: CGFloat = 10
let buttonContainerHeight: CGFloat = 50
let DEFAULT_HEIGHT: CGFloat = 80

private var contentWidth: CGFloat = UIScreen.mainScreen().bounds.width
private var totalContentHeight: CGFloat  = 0.0

// Delegate
var 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
}

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

EDIT: Narrowing down the root of the error, it seems like the UICollectionViewLayoutAttributes returned are only updated to the correct new value if the CV is reloaded first. However, if I do so the batch update throws an error saying the total number of rows AFTER should be equal to before plus the number inserted… which makes sense.

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

EDIT 2: Now I am at a loss. Is the batch update trying to retrieve the layout from the default layout and not my custom layout?

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}'

这怎么可能?

推荐答案

对于那些遇到同样问题的人,我发现当你这样做时

For those who encounter the same issue, I discovered that when you do

self.collectionView?.performBatchUpdates({ () -> Void in
        // add new items into collection
        self.collectionView?.insertItemsAtIndexPaths(indexPaths)

        }, completion: { (finished) -> Void in
            // do insertion animations
    });

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

the layoutAttributesForElementsInRect method is not called and the layoutAttributesForItemAtIndexPathis called instead.

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

Therefore, you must also override layoutAttributesForItemAtIndexPath in the custom layout like so:

override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? {

    // Logic that calculates the UICollectionViewLayoutAttributes of the item
    // and returns the UICollectionViewLayoutAttributes
    return self.singleItemLayout(indexPath)
}

此外,如果您在使用批处理 insertItemsAtIndexPaths 方法时遇到闪烁.

Also, if you encounter flicker when the batch insertItemsAtIndexPaths method on it's own.

这篇关于NSInternalInconsistencyException',原因:'-layoutAttributesForItemAtIndexPath 没有 UICollectionViewLayoutAttributes 实例,自定义布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆