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
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()
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;
}
Related Topics
Maximum Height of iOS 8 Today Extension
How to Put a Logo in Navigationview in Swiftui
How to Disable the Network in iOS Simulator
How to Set Hex Color Code for Background
Silent Push Notifications Only Delivered If Device Is Charging And/Or App Is Foreground
Uicollectionview Auto Scroll to Cell at Indexpath
Dynamic Size Uicollectionview Cell
How to Combine Two Dictionary Instances in Swift
Initializer Does Not Override a Designated Initializer from Its Superclass
Auto Adjust Custom Uitableviewcell and Label in It to the Text
Disabling Nslog for Production in Swift Project
How to Limit an iOS App Only to 4 Inch Screen Devices
iOS 7 Parallax Effect in My View Controller
Email Validation on Textfield in iOS
Uibutton Touch Is Delayed When in Uiscrollview
After Switching to Xcode 7, App Size Grew from 9 Mb to 60 Mb, Is There a Fix