我正在使用 View 并在其中实现一个 Collection View
当我尝试引用数据源并委托(delegate)给我的 colelctionview 时,它给出了错误
我的类文件
class MenuBar : UIView, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout{
let collectionView : UICollectionView = {
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = UIColor.rgb(red: 230, green: 32, blue: 31)
cv.delegate = self // Error here
return cv
}()
override init(frame: CGRect){
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
我无法将委托(delegate)和数据源提供给我的 collectionview 我也实现了以下两种方法
cellForItemAt 索引路径 节中的项目数量
我们将不胜感激
最佳答案
这是因为你试图在闭包中访问 self
lazy var collectionView : UICollectionView = {
[unowned self] in
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = UIColor.rgb(red: 230, green: 32, blue: 31)
cv.delegate = self // Error here
return cv
}()
这应该可行
关于ios - 无法将类型 NSObject -() 类名的值分配给类型 uicollectionviewdelegate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43225309/