no UICollectionViewLayoutAttributes instance for -layoutAttributesForSupplementaryElementOfKind:

前端 未结 2 435
灰色年华
灰色年华 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: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.

提交回复
热议问题