no UICollectionViewLayoutAttributes instance for -layoutAttributesForSupplementaryElementOfKind:

前端 未结 2 415
灰色年华
灰色年华 2021-01-03 12:09

The title is the error I\'m getting and I have no idea why, but here is some information so hopefully someone on here can elucidate me.

I have subclassed UICol

相关标签:
2条回答
  • 2021-01-03 12:13

    I scoured the forums looking for an answer and came across a few suggestions. None of them gave me the succour I needed, and eventually in the interest of meeting deadlines, I dropped the use of supplementary views altogether.

    A few weeks later out of curiosity I looked around again and eventually came across the following post and now I'm back to using supplementary views again.

    So, don't forget to return your:

    - (NSArray<NSIndexPath *> *)indexPathsToDeleteForSupplementaryViewOfKind:(NSString *)elementKind
    {
        return self.removedIndexPaths;
    }
    

    to your collection view layout.

    0 讨论(0)
  • 2021-01-03 12:16

    To prevent a crash you could return dummy attributes for all those indexPaths that are not valid any more. Something like this could help to prevent your crash:

    UICollectionViewLayoutAttributes *layoutAttributes = self.layoutInfo[elementKind][indexPath]; // add some safety checks if this access creates an out of bounds issue
    
    // create dummy layoutAttributes
    // the workaround
    if (layoutAttributes == nil) {
        UICollectionViewLayoutAttributes *dummyLayoutAttributes = [UICollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind:elementKind withIndexPath:indexPath];
        dummyLayoutAttributes.frame = CGRectZero;
        dummyLayoutAttributes.hidden = YES;
        layoutAttributes = dummyLayoutAttributes;
    }
    
    return layoutAttributes;
    

    This still leads to objects in your view stack that should not be there, but they are hidden and don't create a crash. The next time the UICollectionView updates it's layout it should clean out the old hidden views.

    0 讨论(0)
提交回复
热议问题