swift - UICollectionVIew NIL 实例化后

标签 swift xcode uicollectionview

我有一个 UICollectionView,作为导出连接到 Storyboard中的 UICollectionView。我将此作为我的代码的一部分:

override func viewDidLoad()
{
    super.viewDidLoad()
    SetActivityIndicator()

    let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    layout.itemSize = CGSize(width: 80, height: 80)

    ProductsCollection = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)

    ProductsCollection.dataSource = self // Here it says ProductsCollection is nil!
    ProductsCollection.delegate = self

在实例化行之后 ProductsCollection = UICollectionView(frame: self.view.frame, collectionViewLayout: layout) ProductsCollection 为 nil。我究竟做错了什么 ?

编辑:我断开了 socket ,但我仍然在同一行中得到 nil。

异常:

The Exception

最佳答案

如果您不在 Storyboard中创建 Collection View ,那么您需要从代码中的属性声明中删除 weak

另外,变量名以小写字母开头。它应该是 productsCollectionView。确保你也更新了它。

关于swift - UICollectionVIew NIL 实例化后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49105641/

相关文章:

iphone - prepareForSegue 和 popViewController 兼容性

ios - 如何实现 UICollectionView 中页面之间的间距?

通过 [String : Any] and get difference to Struct or Class properties 快速循环

ios - 使用 UICollectionViewController 重新排序的嵌套 Controller

ios - 如何在资源为 5mb 时减小巨大的 .ipa 文件大小

ios - Segue 导致 UITabBarController 在 Swift 中向上移动状态栏?

xcode - 突然出现AppDelegate.h错误

swift - 如何使用 Swift 4.0 获取简短的本地日期/时间格式?

ios - UIMenuController 未在 UICollectionViewController 子类上显示自定义操作

ios - 为什么 CollectionView 中的自定义按钮只加载到最后一个单元格中? ( swift )