I am going through a recurring crash in my iOS app with a UICollectionView. The crash message is as follows:
Error Message:
Thread 1: “Anticipated dequeued view to be returned to the gathering view in preparation for show. When the gathering view’s knowledge supply is requested to offer a view for a given index path, be sure that a single view is dequeued and returned to the gathering view. Keep away from dequeuing views and not using a request from the gathering view. For retrieving an current view within the assortment view, use -[UICollectionView cellForItemAtIndexPath:] or -[UICollectionView supplementaryViewForElementKind:atIndexPath:].”
Background and Setup:
I’m engaged on a venture that entails a UICollectionView that shows information gadgets. I’m utilizing a customized UICollectionViewCell (NewsCollectionViewCell) to characterize every merchandise. The UICollectionView has been registered correctly, and I’m utilizing a typical implementation for dequeuing and configuring cells.
Here is a simplified model of my code:
Code: UICollectionView Cell Setup
class NewsCollectionViewCell: UICollectionViewCell {
static let reuseId = "NewsCollectionViewCell"
personal let imageNews: UIImageView = { ... }()
personal let dateNews: UILabel = { ... }()
personal let nameNews: UILabel = { ... }()
personal let descriptionNews: UILabel = { ... }()
personal let readMoreLabel: UILabel = { ... }()
override init(body: CGRect) {
tremendous.init(body: body)
setupConstraints()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been carried out")
}
override func prepareForReuse() {
tremendous.prepareForReuse()
imageNews.picture = nil
dateNews.textual content = ""
nameNews.textual content = ""
descriptionNews.textual content = ""
}
func fill(with information: Information) {
// Populate the cell with knowledge
}
personal func setupConstraints() {
// SnapKit constraints for structure
}
}
Code: Assortment View Setup and Cell Configuration
extension NewsViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection part: Int) -> Int {
return information.depend
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: NewsCollectionViewCell.reuseId, for: indexPath) as? NewsCollectionViewCell else {
fatalError("Didn't dequeue NewsCollectionViewCell")
}
cell.fill(with: information[indexPath.item])
return cell
}
}
What I’ve Tried So Far:
Checked Reuse Identifier: The reuse identifier for NewsCollectionViewCell is appropriately set and constant in every single place.
Registered Cell Class: The cell is registered in setupCollectionView(), as proven beneath:
personal func setupCollectionView() {
newsCollectionView.delegate = self
newsCollectionView.dataSource = self
newsCollectionView.register(NewsCollectionViewCell.self, forCellWithReuseIdentifier: NewsCollectionViewCell.reuseId)
}
Averted Handbook Reuse: I’ve ensured that I am not manually managing or holding references to dequeued cells.
prepareForReuse: Applied prepareForReuse in NewsCollectionViewCell to reset all properties.
Examined on Completely different Units and iOS Variations: The problem persists throughout totally different gadgets and simulators.
The Drawback:
The crash happens inconsistently, normally when scrolling quickly by way of the gathering view or after a number of reloads. The error message factors to a difficulty with the cell reuse mechanism. Particularly, it means that the cell being dequeued won’t be returned appropriately, or that a number of dequeuing actions may be occurring concurrently.
Questions:
Why does the error nonetheless persist although I am following the usual dequeuing method?
Might there be a difficulty with my customized cell class (NewsCollectionViewCell) or the way it’s being reused?
Is there a greater option to deal with the state of affairs the place dequeuing fails, reasonably than utilizing a fatalError or creating a brand new cell occasion?
Is it attainable that the issue arises from utilizing SnapKit for structure? Ought to I swap to a different method for setting constraints?
I am working out of concepts on what might be inflicting this crash. Any assist or ideas could be enormously appreciated!