【问题标题】:Not able to load a collectionview无法加载集合视图
【发布时间】:2017-11-01 20:30:04
【问题描述】:

我正在故事板中加载collectionview。但是当我运行它时,它没有出现。就在那时,我发现委托和数据源没有设置。但是当我设置委托和数据源并再次运行时,程序崩溃说'could not dequeue a view of kind: UICollectionElementKindCell with identifier prdImgscellIdentifier - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'

似乎我没有错过任何东西..但无法弄清楚为什么会发生崩溃..

这是我的cellForItemAt...

 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! productImagesCollectionViewCell

     cell.myImageView.image = UIImage(named: "appLogo.jpg")

     return cell
 }

【问题讨论】:

标签: ios swift uicollectionview


【解决方案1】:

在 Storyboard 的 CollectionViewCell 中设置单元格标识符为prdImgscellIdentifier

或者可能是因为您尚未将 xib 注册到 UICollectionView。如果你使用 Xib 的 UICollectionViewCell,你必须先注册它。

在 viewDidLoad 中: 写下来:

collectionView.register(UINib(nibName: "name", bundle: nil), forCellWithReuseIdentifier: "cellIdentifier")

【讨论】:

  • 您是否为单元格制作了自定义类?如果是,则将相同的类分配给 Storyboard 中的单元格
  • 我认为,这是因为您尚未将您的 xib 注册到 UICollectionView。如果你使用 Xib 的 UICollectionViewCell,你必须先注册它。在 viewDidLoad: 中写:collectionView.register(UINib(nibName: "name", bundle: nil), forCellWithReuseIdentifier: "prdImgscellIdentifier")
  • 你可以将单元格标识符从 prdImgscellIdentifier 更改为仅限单元格
  • productImagesCollectionViewCell 你是自定义类名吗?你已经在collectionview里面的storyboard中为cell设置了那个类?
【解决方案2】:

您可能需要在 viewDidLoad 中注册您的 customCellClass...请查看此之前的答案以获取完整详细信息:

Swift - could not dequeue a view of kind: UICollectionElementKindCell with identifier

【讨论】:

  • 我直接从情节提要中显示集合视图,我想你建议的链接在我们使用 xib 时很有用...
  • 从那个链接,你有没有尝试@Mohsen的建议在viewDidLoad()注册课程:collectionView.register(productImagesCollectionViewCell.self, forCellWithReuseIdentifier: "prdImgscellIdentifier")
  • 但我是直接从情节提要使用...所以我需要从笔尖注册吗..?
  • 当您将 collectionView.register(productImagesCollectionViewCell.self‌​, forCellWithReuseIdentifier: "prdImgscellIdentifier") 复制并粘贴到具有 cellForItemAt 函数的同一个 .swift 文件中的 viewDidLoad() 代码块中时会发生什么?它还会崩溃吗?
【解决方案3】:

您是否实现了所需的数据源?

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section;

【讨论】:

    【解决方案4】:

    在情节提要上选择您的情节提要,并在实用程序窗口“单元格”中分配一个标识符,如下图所示

    【讨论】:

      猜你喜欢
      • 2013-06-16
      • 1970-01-01
      • 2016-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多