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 {
.....
}
}
extension UICollectionView{
func refreshLayout() {
let oldLayout = collectionViewLayout as! UICollectionViewFlowLayout
let newLayout = UICollectionViewFlowLayout()
newLayout.estimatedItemSize = oldLayout.estimatedItemSize
newLayout.footerReferenceSize = oldLayout.footerReferenceSize
newLayout.headerReferenceSize = oldLayout.headerReferenceSize
newLayout.itemSize = oldLayout.itemSize
newLayout.minimumInteritemSpacing = oldLayout.minimumInteritemSpacing
newLayout.minimumLineSpacing = oldLayout.minimumLineSpacing
newLayout.scrollDirection = oldLayout.scrollDirection
newLayout.sectionFootersPinToVisibleBounds = oldLayout.sectionFootersPinToVisibleBounds
newLayout.sectionHeadersPinToVisibleBounds = oldLayout.sectionHeadersPinToVisibleBounds
newLayout.sectionInset = oldLayout.sectionInset
newLayout.sectionInsetReference = oldLayout.sectionInsetReference
collectionViewLayout = newLayout
}
}
Then call:
YourCollectionView.refreshLayout()
YourCollectionView.reloadData()
Happy Coding!
Received this error when I was accidentally setting a collectionView cell's height to 0. Make sure height (or any dimension) is greater than 0! Making sure it was always greater than 0 solved the issue for me.