Ios Swift-“一个;必须为标识符注册nib或类,或连接故事板中的原型单元;
我一直收到这个错误消息。我已尝试在代码中添加以下内容:Ios Swift-“一个;必须为标识符注册nib或类,或连接故事板中的原型单元;,ios,swift,uicollectionview,Ios,Swift,Uicollectionview,我一直收到这个错误消息。我已尝试在代码中添加以下内容: self.collectionView.register(UICollectionCell.self,forCellReuseIdentifier:“Cell”) 但这只会带来另一个错误: 对成员“collectionView(\uU9:numberOfItemsInSection:)的引用不明确 这是我的密码: class HomeViewController: UIViewController, UICollectionView
self.collectionView.register(UICollectionCell.self,forCellReuseIdentifier:“Cell”)
但这只会带来另一个错误:
对成员“collectionView(\uU9:numberOfItemsInSection:)的引用不明确
这是我的密码:
class HomeViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
@IBOutlet weak var UICollectionView: UICollectionView!
let items = ["0", "1", "2", "3", "4"]
override func viewDidLoad() {
self.UICollectionView.register(CollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
super.viewDidLoad()
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return items.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CollectionViewCell
cell.mainListLabel.text = items[indexPath.item]
return cell
}
}改变
self.collectionView.register(UICollectionCell.self, forCellReuseIdentifier: "Cell")
到
及
CollectionViewCell
代表什么?它是派生类还是什么
因为您注册了UICollectionCell
,但调用了CollectionViewCell
我希望它能起作用
//编辑
最终解决方案:
class CollectionViewCell: UICollectionViewCell {
@IBOutlet weak var mainListButton: UIButton!
@IBOutlet weak var mainListLabel: UILabel!
override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = UIColor.black
}
required init?(coder: NSCoder) {
super.init(coder: coder)
self.backgroundColor = UIColor.black
}
}
设置自定义集合视图类或使用XIB。那么您有表视图还是集合视图?您的代码显示集合视图,但为什么要调用
tableView.register
?添加到Davids注释中,您注册了标识符为“cell”的表视图单元格,并将标识符为“cell”的集合视图单元格出列抱歉,我刚刚复制了代码段。我用collectionView.regester试过了。刚刚更新it@NicolasElPapu我更新的很好。同样的错误您也尝试将UITableViewCell类注册到集合视图中,而不是尝试将“CollectionViewCell”出列,您的意思是self.collectionView.register(CollectionViewCell.self,forCellReuseIdentifier:“Cell”)
。如果force cast有望成功,那么您需要注册实际的类型,而不是它的超类。但我的意思是有三个不同的类在使用UICollectionCell
,UICollectionViewCell
和CollectionViewCell
。我认为没有必要使用强制转换,因为如果让,guard使用起来会非常有用。CollectionViewCell是我创建的一个子类。Idk,但这是如何在教程中完成的,在那里我也有我的标签插座。不管怎样,我还是像你说的那样更改了注册代码,但是我仍然得到了“模糊引用…”错误:(你的UICollectionView是从情节提要中拖拽出来的还是通过编程创建的?@Chris and check out也许你使用了collectionView,然后删除了它,新使用了一个。我的意思是检查连接。
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! UICollectionViewCell
class CollectionViewCell: UICollectionViewCell {
@IBOutlet weak var mainListButton: UIButton!
@IBOutlet weak var mainListLabel: UILabel!
override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = UIColor.black
}
required init?(coder: NSCoder) {
super.init(coder: coder)
self.backgroundColor = UIColor.black
}
}