【问题标题】:How to connect custom collectionviewCell to reuseidentifier with XIB - Swift如何使用 XIB 将自定义 collectionviewCell 连接到重用标识符 - Swift
【发布时间】:2018-12-22 12:12:11
【问题描述】:

我正在使用 XIB 制作自定义 collectionview 单元格。

collectionview 作为扩展放置在 viewController 中。

这是我用来调用 Xib 视图的代码,但我收到一条错误消息,告诉我需要使用重用标识符。但我不知道在使用 XIB 时如何使用它。

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = Bundle.main.loadNibNamed("CustomCell", owner: self, options: nil)?.first as! CustomCell

        return cell
    }

由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“从 -collectionView:cellForItemAtIndexPath: 返回的单元格没有重用标识符 - 必须通过调用 -dequeueReusableCellWithReuseIdentifier:forIndexPath: 来检索单元格” *** 首先抛出调用栈:

【问题讨论】:

    标签: ios swift xib collectionview reuseidentifier


    【解决方案1】:

    首先,您需要为您的单元格创建一个重用标识符。让我们根据您的 collectionViewCell 类名创建它。在你的 ViewController 文件中声明reuseId:

    let reuseId = String(describing: CustomCell.self)
    

    您需要在 viewDidLoad 方法中将您的单元格注册到您的 collectionView 中。

    collectionView.register(CustomCell.self, forCellReuseIdentifier: reuseId)
    

    然后在你的cellForItemAt 方法中:

    guard let cell = collectionView.dequeueReusableCell(withIdentifier: reuseId, for: indexPath) as? CustomCell else { return UICollectionViewCell() }
    //return cell, or update cell elements first.
    

    【讨论】:

      【解决方案2】:

      你可以注册CustomCell点赞,

      let customCellNib = UINib(nibName: "CustomCell", bundle: .main)
      collectionView.register(customCellNib, forCellWithReuseIdentifier: "CustomCell")
      

      并在cellForItemAt中使用相同的注册单元格,

      func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
          guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier:"CustomCell", for: indexPath) as? CustomCell else {
              return UICollectionViewCell()
          }
          return cell
      }
      

      【讨论】:

      • 感谢您的回答。我有另一个问题。如果我有 2 个不同的 customCell 怎么办?假设CustomCell1和CustomCell2。如何注册?
      • same let customCellNib1 = UINib(nibName: "CustomCell1", bundle: .main) collectionView.register(customCellNib1, forCellWithReuseIdentifier: "CustomCell1") let customCellNib2 = UINib(nibName: "CustomCell2", bundle: .main) collectionView.register(customCellNib2, forCellWithReuseIdentifier: "CustomCell2") 并且根据您的要求或逻辑,应在 cellForItemAt 委托方法中返回相同的特定单元格。
      【解决方案3】:

      适用于 Swift 4.0 和 4.2

      在您的 viewDidLoad 中:

      自定义 collectionViewCell

      mainCollectionView.register(UINib(nibName: "your_custom_cell_name", bundle: nil), forCellWithReuseIdentifier: "your_custom_cell_identifier")
      

      在 cellForItemAt 索引路径中:

      let cell : <your_custom_cell_name> = mainCollectionView.dequeueReusableCell(withReuseIdentifier: "your_custom_cell_identifier", for: indexPath) as! <your_custom_cell_name>
      

      别忘了在 xib 部分为您的自定义单元格设置标识符。

      【讨论】:

        猜你喜欢
        • 2018-12-16
        • 2016-04-02
        • 2017-11-05
        • 2017-04-15
        • 1970-01-01
        • 2021-08-27
        • 1970-01-01
        • 1970-01-01
        • 2017-09-12
        相关资源
        最近更新 更多