简体   繁体   English

UICollectionView异常崩溃

[英]UICollectionView Exception Crash

I have a mystery problem with UICollectionView . 我对UICollectionView有一个神秘的问题。

I add my collection view to my tableview cell like so: 我将收藏视图添加到tableview单元中,如下所示:

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];

I'm implementing all the necessary delegate and data source methods and you can see I've set the datasource and delegate and have implemented them in my header. 我正在实现所有必需的委托和数据源方法,您可以看到已经设置了数据源和委托,并在标头中实现了它们。

When I go and press a UIButton which displays a UIImagePickerController and the app crashes with this error: 当我去按下显示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

I have set the datasource, so I have no idea why this would occur. 我已经设置了数据源,所以我不知道为什么会发生这种情况。

I faced with the similar problem, but my app crashed after I programmatically pop my UICollectionViewController. 我遇到了类似的问题,但是当我以编程方式弹出UICollectionViewController之后,我的应用程序崩溃了。 In some reason (I think it's just a bug in SDK) self.collectionView was alive after its' controller destroy, thus causing this failure: 由于某种原因(我认为这只是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'

The solution is just override -dealloc in UICollectionViewController and release self.collectionView manually. 解决方案是在UICollectionViewController中重写-dealloc并手动释放self.collectionView。 ARC code: ARC代码:

- (void)dealloc {

    self.collectionView = nil;
}

Hope this will save time for somebody. 希望这可以节省一些时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM