[ad_1]
I take advantage of UICollectionView to current cells. Every cell takes up the total display screen dimension. The gathering view is created with a number of objects whereas solely the primary one is configured and displayed on the display screen.
The issue is that the second merchandise just isn’t configured nor prefetched until collectionView is scrolled down.
In my use case cell configuration fetches knowledge from distant server, which I favor doing the earlier the higher. After I scroll down however the second cell is not configured there’s nothing to current but.
I suspected that the structure has one thing to do with it, so I attempted to make use of UICollectionViewFlowLayout in addition to UICollectionViewCompositionalLayout and in each instances the issue happens.
I created a demo swift mission with collectionView presenting display screen sized coloured rectangles. Cell configuration and prefetch is logged to the console with their indexPath.
import Basis
import UIKit
enum Part {
case primary
}
typealias DataSource = UICollectionViewDiffableDataSource<Part, UIColor>
typealias Snapshot = NSDiffableDataSourceSnapshot<Part, UIColor>
class CollectionViewController: UIViewController, UICollectionViewDelegateFlowLayout,
UICollectionViewDataSourcePrefetching {
non-public lazy var dataSource: DataSource = makeDataSource()
non-public let collectionView: UICollectionView
non-public static let cellIdentifier = "CollectionViewCell"
init() {
// let structure = Self.makeFlowLayout()
let structure = Self.makeCompositionLayout()
print("Log: collectionview structure - (structure.description)")
collectionView = UICollectionView(body: .zero, collectionViewLayout: structure)
collectionView.register(
UICollectionViewCell.self,
forCellWithReuseIdentifier: Self.cellIdentifier
)
tremendous.init(nibName: nil, bundle: nil)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been applied")
}
override func viewDidLoad() {
tremendous.viewDidLoad()
setupCollectionView()
applySnapshot(animatingDifferences: false)
}
non-public static func makeFlowLayout() -> UICollectionViewFlowLayout {
let flowLayout = UICollectionViewFlowLayout()
flowLayout.scrollDirection = .vertical
flowLayout.minimumLineSpacing = 0
return flowLayout
}
non-public static func makeCompositionLayout() -> UICollectionViewLayout {
let config = UICollectionViewCompositionalLayoutConfiguration()
config.interSectionSpacing = 0
let fullSize = NSCollectionLayoutSize(
widthDimension: .fractionalWidth(1),
heightDimension: .fractionalHeight(1)
)
let merchandise = NSCollectionLayoutItem(layoutSize: fullSize)
let group = NSCollectionLayoutGroup.horizontal(layoutSize: fullSize, subitems: [item])
let structure = NSCollectionLayoutSection(group: group)
return UICollectionViewCompositionalLayout(part: structure, configuration: config)
}
non-public func setupCollectionView() {
collectionView.backgroundColor = .white
collectionView.delegate = self
collectionView.prefetchDataSource = self
collectionView.isPrefetchingEnabled = true
collectionView.contentInsetAdjustmentBehavior = .by no means
view.addSubview(collectionView)
collectionView.translatesAutoresizingMaskIntoConstraints = false
let contraints = [
collectionView.leftAnchor.constraint(equalTo: view.leftAnchor),
collectionView.rightAnchor.constraint(equalTo: view.rightAnchor),
collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
collectionView.topAnchor.constraint(equalTo: view.topAnchor)
]
NSLayoutConstraint.activate(contraints)
}
non-public func makeDataSource() -> DataSource {
let dataSource = DataSource(
collectionView: collectionView,
cellProvider: { (collectionView, indexPath, itemIdentifier) -> UICollectionViewCell? in
let cell = collectionView.dequeueReusableCell(
withReuseIdentifier: Self.cellIdentifier,
for: indexPath
)
print("Log: configure cell in indexPath - (indexPath)")
cell.backgroundColor = itemIdentifier
return cell
})
return dataSource
}
non-public func applySnapshot(animatingDifferences: Bool = true) {
let objects = (0...200).map { _ in
UIColor(
crimson: CGFloat.random(in: 0...1),
inexperienced: CGFloat.random(in: 0...1),
blue: CGFloat.random(in: 0...1),
alpha: 1
)
}
var snapshot = Snapshot()
snapshot.appendSections([.main])
snapshot.appendItems(objects)
dataSource.apply(snapshot, animatingDifferences: animatingDifferences)
}
// MARK: - UICollectionViewDelegateFlowLayout
func collectionView(
_ collectionView: UICollectionView,
structure collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath
) -> CGSize {
return CGSize(width: collectionView.bounds.width, top: collectionView.bounds.top)
}
// MARK: - UICollectionViewDataSourcePrefetching
func collectionView(_ collectionView: UICollectionView, prefetchItemsAt indexPaths: [IndexPath]) {
print("Log: prefetch cells in indexPaths - (indexPaths)")
}
}
[ad_2]
