【问题标题】:iOS Assertion Failure in UICollectionViewUICollectionView 中的 iOS 断言失败
【发布时间】:2012-09-17 23:15:09
【问题描述】:

我收到了错误...

*** Assertion failure in -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:], /SourceCache/UIKit/UIKit-2372/UICollectionView.m:2249

尝试显示 UICollectionView 时。

导致它的行是......

static NSString *CellIdentifier = @"Cell";

UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];

出队发生错误。

没有其他错误,所以我很难知道从哪里开始。

有人能解释一下吗?

【问题讨论】:

    标签: iphone ios uicollectionview


    【解决方案1】:

    您需要像下面这样注册:

    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"MY_CELL"];
    

    【讨论】:

      【解决方案2】:

      一直在阅读文档(应该先这样做:))

      无论如何,我正在使用的 collectionView 位于单独的 xib 文件(不是故事板)中,并且来自文档...

      Important: You must register a class or nib file using the
      registerClass:forCellWithReuseIdentifier: or
      registerNib:forCellWithReuseIdentifier: method before calling this method.
      

      谢谢

      【讨论】:

      • 什么时候调用这个方法?在“UICollectionViewCell *cell = [collectionView dequeue...”之前的行上或使用其他方法
      • 我必须在 viewDidLoad 方法中注册它。您只需为整个 collectionView 注册一次 xib。然后,当您调用 dequeueCellWithIdentifier 时,它会转到您注册的 xib。
      • 我遇到了同样的问题,但由于某种奇怪的原因,编译器无法识别viewDidLoad 中的registerClass 方法,所以我不得不将其移至cellForItemAtIndexPath 方法。
      • @Sam:原因可能是当您在viewDidLoad 中调用registerClass:forCellWithReuseIdentifier: 方法时尚未分配您的UICollectionView 对象。如果您的集合视图是通过 Interface Builder 定义的,则不会发生这种情况,因为 viewDidLoad初始化集合视图之后被调用。如果您以编程方式初始化集合视图,只需确保在 调用 registerClass:... 方法之前对其进行分配。我不认为cellForItemAtIndexPath 是适合它的地方...
      • 谢谢!你节省了我的时间:)
      【解决方案3】:

      我遇到了同样的问题。这是我解决它的方法。

      移动

      [self.pictureCollectionView registerNib:[UINib nibWithNibName: bundle:nil] forCellWithReuseIdentifier:reuseID]

      - (void)viewDidLoad

      而不是方法- (void)awakeFromNib

      【讨论】:

        【解决方案4】:

        确保如果您使用registerNib: 方法:

        UINib *nibH = [UINib nibWithNibName:HEADER_ID bundle:nil];
        [collectionView registerNib:nibH
         forSupplementaryViewOfKind:UICollectionElementKindSectionHeader 
                withReuseIdentifier:HEADER_ID];
        

        ALSO 在 nib 文件中,当您选择顶级集合可重用视图时,使用属性检查器,并确保 Identifier 设置为与您传递给 withReuseIdentifier: 参数的值相同。

        【讨论】:

          【解决方案5】:

          我遇到了这个仅在 iOS 9 上崩溃(iOS 10/11 运行良好)。

          我没有流布局的自定义子类,而是直接在现有布局上设置headerReferenceSize。 因此,在启用 Section Header 的 Interface Builder 中,我遇到了这个崩溃,没有复选标记一切正常,并且标题显示正确,因为我在代码中设置了大小。

          【讨论】:

          • 也许很少有应用程序支持 iOS 9 了:D。但很高兴它有帮助!
          【解决方案6】:

          替换

          NSString *CellIdentifier = @"Cell";
          

          static NSString *CellIdentifier = @"Cell";
          

          【讨论】:

          • 先试过了。我刚刚在阅读文档。我需要在运行 dequeue 之前注册一个类或 nib...
          【解决方案7】:

          当使用具有唯一 ReuseIdentifiers 的多个 UICollectionViews 时,我看到此错误弹出。在 ViewDidLoad 你想像这样注册每个 CollectionView 的重用标识符:

          [_collectionView1 registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"collectionView1CellIdentifier"];
          [_collectionView2 registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"collectionView2CellIdentifier"];
          

          然后,当您到达“- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath”时,您要确保不要尝试将collectionView1 的单元格设置为reuseIdentifier collectionView2 否则你会得到这个错误。

          不要这样做:(或者 collectionView2 会看到错误的标识符并在看到它所期望的标识符之前抛出一个合适的结果)

          UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionView1CellIdentifier" forIndexPath:indexPath];
          
          if(collectionView != _collectionView1){
             cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionView2CellIdentifier" forIndexPath:indexPath];
          }
          
          cell.backgroundColor = [UIColor greenColor];
          return cell;
          

          这样做

          UICollectionViewCell *cell;
          
          if(collectionView == _collectionView1){
              cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionView1CellIdentifier" forIndexPath:indexPath];
          }else{
             cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionView2CellIdentifier" forIndexPath:indexPath];
          }
          
          cell.backgroundColor = [UIColor greenColor];
          return cell;
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-10-04
            • 1970-01-01
            • 2017-01-13
            • 1970-01-01
            相关资源
            最近更新 更多