我的目标是创建一个在每个单元格上具有3种不同类型的集合视图单元格的集合视图。
但是我从Xcode收到此错误:
“无法使同类视图出队:带有标识符命令的UICollectionElementKindCell-必须为该标识符注册一个笔尖或一个类,或者在情节提要中连接原型单元格”
我更改了寄存器上的标识符以匹配单元格上的标识符,但是仍然出现错误。我的代码有什么问题,我该如何解决?
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")
}
}
答案 0 :(得分:1)
否,如果您必须在setupViews
中全部注册
collectionViews.register(oneCollectionViewCell.self, forCellWithReuseIdentifier: "one")
collectionViews.register(commandsCollectionViewCell.self, forCellWithReuseIdentifier: "commmands")
collectionViews.register(threesCollectionViewCell.self, forCellWithReuseIdentifier: "threes")
//
此
collectionViews.register(commandsCollectionViewCell.self, forCellWithReuseIdentifier: "commmands")
应该bt
collectionViews.register(commandsCollectionViewCell.self, forCellWithReuseIdentifier: "commands")