UICollectionView recieved layout attributes for a cell with an index path that does not exist

后端 未结 3 1290
面向向阳花
面向向阳花 2021-01-14 12:21

I have used UICollection view to show items in grid layout.

For data source I have use 5*5 dimensional array.

And also I am returning 5 for numberOfItems in

相关标签:
3条回答
  • 2021-01-14 12:50

    Just in case others find this question with Google - I received the same error with a very interesting index path:

    'NSInternalInconsistencyException', reason: 'UICollectionView received layout attributes for a cell with an index path that does not exist: {length = 2, path = 0 - 0}'

    I had simply forgotten to connect the collection view's data source and delegate to the view controller in Interface Builder. D'oh!

    0 讨论(0)
  • 2021-01-14 12:53

    It works for me.

    
        collectionView.reloadData()
        collectionView.collectionViewLayout.invalidateLayout()
    
    

    It comes because of the cache of the Cell's Autolayout when reloadData.

    0 讨论(0)
  • 2021-01-14 12:58

    The MultipleLineLayout was originally written for infinite scrolling, so there was a problem with that implementation for your use. It should look like this,

    -(NSArray*)layoutAttributesForElementsInRect:(CGRect)rect {
    
        NSMutableArray* attributes = [NSMutableArray array];
        for(NSInteger i=0 ; i < self.collectionView.numberOfSections; i++) {
            for (NSInteger j=0 ; j < [self.collectionView numberOfItemsInSection:i]; j++) {
                NSIndexPath* indexPath = [NSIndexPath indexPathForItem:j inSection:i];
                [attributes addObject:[self layoutAttributesForItemAtIndexPath:indexPath]];
            }
        }
        return attributes;
    }
    
    0 讨论(0)
提交回复
热议问题