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

标签 ios swift uicollectionviewdelegate

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

相关文章:

ios - 我怎样才能在ios中进入应用程序起始页

ios - 为数字添加逗号分隔符,键入时每隔三个数字

ios - 如何在 NWConnection 中设置 UDP 广播的发送方端口

ios - 我需要两个 TableViewController 吗?

ios - collectionView :didSelectItemAtIndexPath does not get called on tap

swift - 以编程方式创建自定义 UICollectionFlowLayout

ios - 如何在 UICollectionViewSectionHeader 中嵌入 UICollectionView

ios - 通过 iOS 将用户和密码发送到网页

ios - 如何避免 "Open this page in ' appName'"警报出现在 iOS 版 Safari 中?

Swift:获取 JPEG 图像的正确文件大小