Uicollectionview Received Layout Attributes for a Cell with an Index Path That Does Not Exist: <Nsindexpath: 0X79Fe0F20> {Length = 2, Path = 0 - 4}

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

I've faced the same problem. Here an explanation: if you use a custom collectionViewLayout and you have a cache for layout attributes (best practice so you don't have to calculate every time attributes), you have to override invalidateLayout method in your custom layout class and purge your cache.

Here's my layout attributes array

private var cache = [UICollectionViewLayoutAttributes]()

Here the overrided method

    override func invalidateLayout() {
super.invalidateLayout()
cache.removeAll()
}

I call layout invalidation in my textDidChange delegate

    func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
if searchText.characters.count > 0 {
// search and reload data source
self.searchBarActive = true
self.filterContentForSearchText(searchText)
self.collectionView?.collectionViewLayout.invalidateLayout()
self.collectionView?.reloadData()
}else{
self.searchBarActive = false
self.collectionView?.collectionViewLayout.invalidateLayout()
self.collectionView?.reloadData()
}
}

iOS 10 bug: UICollectionView received layout attributes for a cell with an index path that does not exist

This happened to me when number of cells in collectionView changed. Turns out I was missing invalidateLayout after calling reloadData. After adding it, I haven't experienced any more crashes. Apple has made some modifications to collectionViews in iOS10. I guess that's the reason why we are not experiencing same problem on older versions.

Here's my final code:

[self.collectionView reloadData];
[self.collectionView.collectionViewLayout invalidateLayout];

//Swift 4.2 Version
collectionView.reloadData()
collectionView.collectionViewLayout.invalidateLayout()

Error: UICollectionView received layout attributes for a cell with an index path that does not exist in Swift

There is prepare function in PinterestLayout.swift as it should be because it is the only layout used for customizing the collectionView.

At line 76, in PinterestLayout.swift file.

Try

override public func prepare() {
cache.removeAll()
if cache.isEmpty {
.....
}
}

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

I've faced the same problem. Here an explanation: if you use a custom collectionViewLayout and you have a cache for layout attributes (best practice so you don't have to calculate every time attributes), you have to override invalidateLayout method in your custom layout class and purge your cache.

Here's my layout attributes array

private var cache = [UICollectionViewLayoutAttributes]()

Here the overrided method

    override func invalidateLayout() {
super.invalidateLayout()
cache.removeAll()
}

I call layout invalidation in my textDidChange delegate

    func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
if searchText.characters.count > 0 {
// search and reload data source
self.searchBarActive = true
self.filterContentForSearchText(searchText)
self.collectionView?.collectionViewLayout.invalidateLayout()
self.collectionView?.reloadData()
}else{
self.searchBarActive = false
self.collectionView?.collectionViewLayout.invalidateLayout()
self.collectionView?.reloadData()
}
}

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

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

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

Try to swap reloadData() and invalidateLayout(), and additionally do not call layoutSubviews directly (see docs)

cltnEdits.collectionViewLayout.invalidateLayout()
cltnEdits.reloadData()
cltnEdits.setNeedsLayout()
cltnEdits.layoutIfNeeded()

cltnTools.collectionViewLayout.invalidateLayout()
cltnTools.reloadData()
cltnTools.setNeedsLayout()
cltnTools.layoutIfNeeded()


Related Topics



Leave a reply



Submit