Reputation:
My goal is to create a collection view with 3 different types of collection view cells on each of it cell.
But I get this error from Xcode:
'could not dequeue a view of kind: UICollectionElementKindCell with identifier commands - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
I have changed the identifier on register to match the identifier on cell, but I still get an error. What is wrong with my code and how do I fix this?
class mainCollectionView: UIView,UICollectionViewDelegateFlowLayout,UICollectionViewDataSource{
var indexNumber:Int = 0
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 3
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if indexPath.row == 0 {
indexNumber = indexPath.row
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "one", for: indexPath) as! oneCollectionViewCell
cell.backgroundColor = .blue
return cell
}else if indexPath.row == 1{
indexNumber = indexPath.row
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "commands", for: indexPath) as! commandsCollectionViewCell
cell.backgroundColor = .red
return cell
}else{
indexNumber = indexPath.row
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "threes", for: indexPath) as! threesCollectionViewCell
cell.backgroundColor = .green
return cell
}
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: self.frame.width, height: self.frame.height)
}
lazy var collectionViews: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = UIColor.clear
cv.dataSource = self
cv.delegate = self
return cv
}()
override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
}
func setupViews(){
if indexNumber == 0 {
collectionViews.register(oneCollectionViewCell.self, forCellWithReuseIdentifier: "one")
}else if indexNumber == 1 {
collectionViews.register(commandsCollectionViewCell.self, forCellWithReuseIdentifier: "commmands")
}else {
collectionViews.register(threesCollectionViewCell.self, forCellWithReuseIdentifier: "threes")
}
collectionViews.translatesAutoresizingMaskIntoConstraints = false
backgroundColor = UIColor.clear
addSubview(collectionViews)
collectionViews.topAnchor.constraint(equalTo: topAnchor, constant: 0).isActive = true
collectionViews.bottomAnchor.constraint(equalTo: bottomAnchor, constant: 0).isActive = true
collectionViews.centerYAnchor.constraint(equalTo: centerYAnchor, constant: 0).isActive = true
collectionViews.centerXAnchor.constraint(equalTo: centerXAnchor, constant: 0).isActive = true
collectionViews.widthAnchor.constraint(equalTo: widthAnchor, constant: 0).isActive = true
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
Upvotes: 0
Views: 86
Reputation: 100503
No if here you must register all in setupViews
collectionViews.register(oneCollectionViewCell.self, forCellWithReuseIdentifier: "one")
collectionViews.register(commandsCollectionViewCell.self, forCellWithReuseIdentifier: "commmands")
collectionViews.register(threesCollectionViewCell.self, forCellWithReuseIdentifier: "threes")
//
This
collectionViews.register(commandsCollectionViewCell.self, forCellWithReuseIdentifier: "commmands")
should bt
collectionViews.register(commandsCollectionViewCell.self, forCellWithReuseIdentifier: "commands")
Upvotes: 1