diff --git a/FinniversKit/Sources/Recycling/GridViews/AdRecommendations/GridView/AdRecommendationsGridView.swift b/FinniversKit/Sources/Recycling/GridViews/AdRecommendations/GridView/AdRecommendationsGridView.swift index 33d8c853a5..074d0d379a 100644 --- a/FinniversKit/Sources/Recycling/GridViews/AdRecommendations/GridView/AdRecommendationsGridView.swift +++ b/FinniversKit/Sources/Recycling/GridViews/AdRecommendations/GridView/AdRecommendationsGridView.swift @@ -1,7 +1,3 @@ -// -// Copyright © FINN.no AS, Inc. All rights reserved. -// - import UIKit public protocol AdRecommendationsGridViewDelegate: AnyObject { @@ -51,15 +47,14 @@ public class AdRecommendationsGridView: UIView { private let imageCache = ImageMemoryCache() - private lazy var collectionViewLayout: AdRecommendationsGridViewLayout = { + private func collectionViewLayout() -> AdRecommendationsGridViewLayout { let layout = AdRecommendationsGridViewLayout() layout.delegate = self return layout - }() + } - // Have the collection view be private so nobody messes with it. public private(set) lazy var collectionView: UICollectionView = { - let collectionView = UICollectionView(frame: .zero, collectionViewLayout: collectionViewLayout) + let collectionView = UICollectionView(frame: .zero, collectionViewLayout: collectionViewLayout()) collectionView.translatesAutoresizingMaskIntoConstraints = false collectionView.delegate = self collectionView.dataSource = self diff --git a/FinniversKit/Sources/Recycling/GridViews/AdRecommendations/GridView/AdRecommendationsGridViewLayout.swift b/FinniversKit/Sources/Recycling/GridViews/AdRecommendations/GridView/AdRecommendationsGridViewLayout.swift index 77e16c0ba6..7216ff0d49 100644 --- a/FinniversKit/Sources/Recycling/GridViews/AdRecommendations/GridView/AdRecommendationsGridViewLayout.swift +++ b/FinniversKit/Sources/Recycling/GridViews/AdRecommendations/GridView/AdRecommendationsGridViewLayout.swift @@ -1,7 +1,3 @@ -// -// Copyright © FINN.no AS, Inc. All rights reserved. -// - import UIKit protocol AdRecommendationsGridViewLayoutDelegate: AnyObject { @@ -75,7 +71,7 @@ class AdRecommendationsGridViewLayout: UICollectionViewLayout { override func prepare() { super.prepare() - itemAttributes = [UICollectionViewLayoutAttributes]() + itemAttributes.removeAll() guard let collectionView = collectionView else { return @@ -122,7 +118,13 @@ class AdRecommendationsGridViewLayout: UICollectionViewLayout { } override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? { - return itemAttributes[indexPath.row] + guard itemAttributes.indices.contains(indexPath.row) + else { return nil } + if itemAttributes[indexPath.row].representedElementCategory == .cell { + return itemAttributes[indexPath.row] + } else { + return nil + } } override var collectionViewContentSize: CGSize {