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
Parametrized Unit Tests in Swift
How to Use The Delegates with Nskeyedunarchiver
Move Button When Keyboard Appears Swift
Why Would One Use Nested Classes
Type 'Int' Does Not Conform to Protocol 'Booleantype'
Check If Class Has a Value for a Key
How to Specify Japanese Encoding for a UIlabel
How to Initialize a Struct with a Closure Parameter Like This
Swift: Make Two Types with The Same "Shape" Conform to a Common Protocol
Xcode 9.2 Is Not Showing Swift 4.1
Error "[Sharesheet] Connection Invalidated" Error iOS13+ But Not on iOS 11.4
Wait for Download Task to Finish in Nsurlsession
Why How to Make Same-Type Requirement in Swift with Generics? Is There Any Way
How to Test If an Instance Is a Specific Class or Type in Swift
Swift Version Build Configuration
Swift: Rsa Encrypt a String with a Specific Private Key
Hide Header While Collection View Is Loading
Arkit - Getting "Unexpectedly Found Nil" When Using Scn File > 300 Mb