gpt4 book ai didi

ios - 无法将类型 NSObject -() 类名的值分配给类型 uicollectionviewdelegate

转载 作者:行者123 更新时间:2023-11-28 10:56:22 25 4
gpt4 key购买 nike

我正在使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com