【问题标题】:Error could not dequeue a view of kind UICollectionElementKindCell错误无法使 UICollectionElementKindCell 类型的视图出列
【发布时间】:2013-02-25 01:55:12
【问题描述】:

首先使用集合视图并遇到此错误:

由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无法将类型视图出列:带有标识符 Cell 的 UICollectionElementKindCell - 必须为标识符注册一个 nib 或一个类,或者在情节提要中连接一个原型单元格”

代码很简单,如下图。我一生都无法弄清楚我错过了什么。

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor whiteColor]; 
return cell;
}

集合视图控制器是使用 nib 创建的,并且委托和数据源都设置为文件的所有者。

View Controller 的头文件也很基础。

@interface NewMobialViewController_3 : UICollectionViewController <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@end

【问题讨论】:

标签: ios objective-c uicollectionview


【解决方案1】:

来自UICollectionView documentation 的出队方法:

重要提示:在调用此方法之前,您必须使用 registerClass:forCellWithReuseIdentifier: 或 registerNib:forCellWithReuseIdentifier: 方法注册类或 nib 文件。

【讨论】:

  • 如果我使用默认的 UICollectionViewCell 而不是自定义的,我该怎么办
  • 您注册的是基类而不是自定义类。但是你几乎总是想要一个子类。默认单元格中没有任何内容。
【解决方案2】:

您需要在 dequeueReusableCellWithReuseIdentifier 的参数和 UICollectionViewCell 的属性之间使用相同的标识符。

UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionViewCell" forIndexPath:indexPath];

【讨论】:

  • 如何为添加UICollectionView的UITableViewCell注册Identifier?
【解决方案3】:

补充@jrtuton 写的内容...您需要的是:

1) 注册您的 nib 文件以将其与您的标识符“连接”:

//MyCollectionView.m
- (void) awakeFromNib{
 [self registerNib:[UINib nibWithNibName:@"NibFileName" bundle:nil]   forCellWithReuseIdentifier: @"MyCellIdentifier"];
}

2) 使用您的标识符从笔尖加载您的自定义单元格:

//MyCollectionView.m
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath  *)indexPath {
    MyCustomCollectionViewCell* cell = [cv dequeueReusableCellWithReuseIdentifier:@"MyCellIdentifier" forIndexPath:indexPath];
}

3) 始终使用静态 NSString* 以避免在您的应用中再次出现相同的标识符。

【讨论】:

    【解决方案4】:

    我已经 100% 正确完成了所有工作。 但由于某种原因,我在一个小时内遇到了同样的错误,然后我所做的是:

    1. 转到故事板
    2. 选择原型单元(在我的例子中)
    3. 从身份检查器中清除类名,然后重写它
    4. 在属性检查器中重写标识符名称

    简单地说,即使我之前做对了,也重做这一步,就像 xcode 在特定的纳秒内遗漏了一些东西!

    【讨论】:

      【解决方案5】:

      斯威夫特 5

      1) 确保 HEADER 具有正确的双端队列,您可能正在使用常规单元格。

      override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
              let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: headerId, for: indexPath)
              return header
          }
      

      2)双重检查注册(ViewDidLoad)

      collectionView.register(HeaderCell.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: headerId)
      

      【讨论】:

      • 谢谢...标头的注册不同。
      【解决方案6】:

      Swift4.0

      当您的UITableViewCellxib 时,您必须向UITableView 注册。

      override func viewDidLoad(){
          super.viewDidLoad()
      
          self.yourtableview.register(UINib(nibName: "yourCellXIBname", bundle: Bundle.main), forCellReuseIdentifier: "YourCellReUseIdentifier")
         }
      

      【讨论】:

        【解决方案7】:

        如果项目有多个 ViewController,请务必检查 ViewController 自定义类是否正确。然后确保 "collectionView.dequeueReusableCell(withReuseIdentifier: "ABC", for: indexPath) as?"与 Collection View 内 Collection Reusable View 中使用的标识符匹配。

        【讨论】:

          【解决方案8】:

          如果您使用 storyboard 而不是 xib,这里有几个步骤。

          1. 确保填写正确的单元格标识符。

          1. 在ColletionViewDelegate中。

            func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
                let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "YourCellName", for: indexPath)as! YourCellName
                return cell            
            }
            
          2. 就是这样。你也不想想添加 registerClass:forCellWithReuseIdentifier: 这会导致元素 nil 错误。

          【讨论】:

            【解决方案9】:

            您需要在情节提要中提供正确的可重用标识符,您在注册 collectionViewCell 时已在代码中提供了该标识符。

            这里是代码。

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

            【讨论】:

              【解决方案10】:

              解决这个问题的方法与 Omar 的回答(大致)相同,除了我最终创建了一个新的 UICollectionViewCell 自定义类并为单元重用标识符提供了一个新的/不同的名称,而不是我在应用程序中其他地方使用的名称。之后立即生效。

              【讨论】:

              • 如果这与上下文无关 - 这是为方便起见重新发布的 Omar 的答案: - 转到情节提要 - 选择原型单元(在我的情况下) - 从身份检查器中清除类名,然后重写它 - 在属性检查器中重写标识符名称
              【解决方案11】:

              以防万一,如果您在 Attributes Inspector -> Identifier 字段的正确位置使用情节提要设置 collectionView 标识符。不在“Restoration ID”中的类名下。

              如果您在 tableView 单元格中使用集合视图,请将委托添加到不在 tableViewController 中的 tableView 单元格。

              【讨论】:

                【解决方案12】:

                我遇到了同样的问题。
                当我创建一个 CollectionViewController 时,默认的reuseIdentifierCell,大写为C,但我犯了一个错误,将Storyboard 中的单元格标识符设置为cell
                它们必须相同。

                【讨论】:

                  【解决方案13】:

                  如果您曾经尝试解决另一个问题的解决方案,但代码中的语法正确,插座 UI 正确。

                  你应该检查你的 tableview 或 collectionview 然后检查你是否忘记了 set delegate "tableview.delegate = self or collectionview.delegate = self"

                  collectionview.delegate = self
                  collectionview.dataSource = self
                  

                  在我的情况下,只有您在单元格中设计集合重叠集合。

                  谢谢。

                  【讨论】:

                    【解决方案14】:

                    我知道这是一个旧问题,但我遇到了同样的问题,并想分享为我解决问题的方法。

                    在我的例子中,我声明了一个全局标识符

                    let identifier = "CollectionViewCell"
                    

                    并且必须在使用前使用 self

                    collectionView.dequeueReusableCellWithReuseIdentifier(self.identifier, forIndexPath: indexPath) as! CollectionViewCell
                    

                    希望这可以帮助某人:)

                    【讨论】:

                      【解决方案15】:

                      如果通过代码创建,则必须创建一个自定义UICollectionViewCell,然后,初始化一个UICollectionView,并使用loadView设置视图是你创建的UICollectionView。如果您在 viewDidload() 中创建,则它不起作用。另外,你还可以拖一个UICollectionViewController,这样可以节省很多时间。

                      【讨论】:

                        猜你喜欢
                        • 1970-01-01
                        • 2017-03-07
                        • 1970-01-01
                        • 2023-04-04
                        • 2015-10-13
                        • 2017-11-22
                        • 2012-12-25
                        • 2011-01-28
                        • 1970-01-01
                        相关资源
                        最近更新 更多