【问题标题】:UICollectionView Exception CrashUICollectionView 异常崩溃
【发布时间】:2013-08-15 13:45:16
【问题描述】:

UICollectionView 有一个神秘问题。

我将我的收藏视图添加到我的表格视图单元格中,如下所示:

UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
                layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

                UICollectionView *collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100) collectionViewLayout:layout];
                [collection setDataSource:self];
                [collection setDelegate:self];
                [collection setBackgroundColor:[UIColor whiteColor]];
                [collection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
                collection.tag = kTagPhotoCollectionView;
                [cell.contentView addSubview:collection];

我正在实现所有必要的委托和数据源方法,您可以看到我已经设置了数据源和委托,并在我的标题中实现了它们。

当我按下显示UIImagePickerControllerUIButton 时,应用程序因此错误而崩溃:

2013-08-13 20:17:02.578 [619:60b] *** Assertion failure in -[PUCollectionView _createPreparedSupplementaryViewForElementOfKind:atIndexPath:withLayoutAttributes:applyAttributes:], /SourceCache/UIKit/UIKit-2891/UICollectionView.m:1397
2013-08-13 20:17:35.502 Sparky[619:60b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UICollectionView dataSource is not set'
*** First throw call stack:
(0x30a02ed3 0x3aa2b6c7 0x30a02da5 0x3138c72f 0x3305f647 0x32f4adf9 0x32f47139 0x32eea543 0x32b72feb 0x32b6e817 0x32b6e6a9 0x32b6e0bd 0x32b6decf 0x32b67bfd 0x309cdf71 0x309cb8ff 0x309cbc4b 0x30936541 0x30936323 0x353ca343 0x32f4f8a5 0x5f835 0x3af40ab7)
libc++abi.dylib: terminating with uncaught exception of type NSException

我已经设置了数据源,所以我不知道为什么会发生这种情况。

【问题讨论】:

  • PUCollectionView 是什么?错误引用了这一点,但我在您的代码中看不到它的任何用途?
  • 我不知道它是什么。我认为它是苹果在幕后使用的一些隐藏类。
  • 如果把呈现图片选择器的按钮方法中的所有代码都注释掉,还会崩溃吗?会不会是您的控制器(集合视图的委托)正在被释放?
  • 好吧,你把我带到了某个地方。当我只在现有视图控制器上显示视图控制器时,为什么会出现这种情况?建议?感谢您迄今为止的帮助。
  • 我不知道,您需要描述您拥有哪些控制器,以及如何将它们显示在屏幕上。您可以检查它是否正在被释放,只需实现 dealloc 并在其中放置一条日志语句。

标签: ios objective-c datasource uicollectionview


【解决方案1】:

我遇到了类似的问题,但是在我以编程方式弹出 UICollectionViewController 后,我的应用程序崩溃了。出于某种原因(我认为这只是 SDK 中的一个错误)self.collectionView 在其控制器销毁后仍然存在,从而导致此故障:

*** Assertion failure in -[UICollectionView _createPreparedSupplementaryViewForElementOfKind:atIndexPath:withLayoutAttributes:applyAttributes:], /SourceCache/UIKit/UIKit-2935.137/UICollectionView.m:1305
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UICollectionView dataSource is not set'

解决方案只是覆盖 UICollectionViewController 中的 -dealloc 并手动释放 self.collectionView。弧码:

- (void)dealloc {

    self.collectionView = nil;
}

希望这会为某人节省时间。

【讨论】:

    猜你喜欢
    • 2019-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-01
    • 1970-01-01
    相关资源
    最近更新 更多