简体   繁体   中英

UICollectionView Exception Crash

I have a mystery problem with UICollectionView .

I add my collection view to my tableview cell like so:

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:

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. In some reason (I think it's just a bug in SDK) self.collectionView was alive after its' controller destroy, thus causing this failure:

*** 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. ARC code:

- (void)dealloc {

    self.collectionView = nil;
}

Hope this will save time for somebody.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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