In my swift app I've a collection view with a lot of items, I want to scroll to an item and then set a propriety of the collectionview cell custom class, but the cast fails, why? Here's my code, I've omitted some obviously steps, but the result is printing of "Cast fails":
class ViewController: UIViewController {
lazy var collectionView: UICollectionView = {
let cv = UICollectionView(frame: self.view.bounds, collectionViewLayout: UICollectionViewFlowLayout())
cv.backgroundColor = .gray
cv.register(Cell.self, forCellWithReuseIdentifier: Cell.id)
cv.delegate = self
cv.dataSource = self
return cv
}()
}
extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: Cell.id, for: indexPath) as! Cell
cell.backgroundColor = .purple
cell.label.text = "\(indexPath)"
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if indexPath.item == 14 {
collectionView.scrollToItem(at: IndexPath(item: 1, section: 0), at: .top, animated: true)
guard let cell = collectionView.cellForItem(at: IndexPath(item: 1, section: 0)) as? Cell else {
print("Cast fails")
return
}
cell.label.text = "Something"
}
}
}
Here's the solution I found:
extension UICollectionView {
func scrollToItem(at indexPath: IndexPath, at position: UICollectionView.ScrollPosition, completion: @escaping () -> ()) {
scrollToItem(at: indexPath, at: .top, animated: true)
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
completion()
}
}
}
The cast fails as the cell ( of that specific index ) is not visible at the moment you query for it
guard let cell = collectionView.cellForItem(at: IndexPath(item: 1, section: 0)) as? Cell else {
print("Cast fails")
return
}
instead you can try to set , at: .top, animated: false)
or wrap the above code snippet inside a Dispatch after block
Btw you should better change the dataSource array and reload that index and that way you don't need a wait
// change source array at that index
// scroll to that row with/without animation
// make sure you set value for the cell label in cellForRowAt table's datasource method