Thanks for your replies.
This is the code that is thowing the "Could not cast" error. There is only one cell in storyboard.
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! PortfolioCollectionViewCell
print("class =", String(describing: type(of: cell)))
cell.cellImageView.image = UIImage(named: imageList[indexPath.item])
return cell
}
The resuse identifier is declared thusly:
private let reuseIdentifier = "portfolioCell"
Copying and pasting from Collection resuable view: Identifier:
portfolioCell
Copying and pasting from the Custom Class of the cell:
PortfolioCollectionViewCell, where "inherit module from target" is checked.
The declaration of that class is:
class PortfolioCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var cellImageView: UIImageView!
}
Everything seems to be correct, and it was working (iirc) before my little mishap with Main.storyboard. I suspect that there may be some namespace issue going on. I have completely deleted and reconstructed that UICollectionViewController but I am getting the same error.
I just did some more debugging. I changed the cell For Item function to this:
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)
print("class =", String(describing: type(of: cell)))
print("cell.reuseIdentifier =", cell.reuseIdentifier ?? "No identifier")
if reuseIdentifier != cell.reuseIdentifier {
print("reuseidentifiers not equal")
}
return cell
}
and got this:
class = UICollectionViewCell
cell.reuseIdentifier = portfolioCell
class = UICollectionViewCell
cell.reuseIdentifier = portfolioCell
class = UICollectionViewCell
cell.reuseIdentifier = portfolioCell
So the identifiers match, but the assigned class is not being picked up.
This is a bit strange! Any ideas?
---
Mark