Вопрос

Although I seem to be able to set up a dataSource onto a UICollectionView just fine in my view controller:

- (void)setupCollectionView:(UICollectionView*)collectionView {
    CustomCollectionViewDataSource *dataSource = [[CustomCollectionViewDataSource alloc] initWithCollectionView: collectionView];
    [collectionView setDataSource: dataSource];
    [collectionView setDelegate: self];
    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:kNNReuseIdentifier];
}

When I log collectionView, dataSource, and collectionView.dataSource, they all look exactly like they are supposed to. I only have one section in my collection view, so I don't implement numberOfSectionsInCollectionView in my data source.

Once my collection view loads, I get the app crashes. There is no related log for the crash with or without breakpoint exceptions on. When exception breakpoints are on, it doesn't point me to where my code is causing the problem. Upon crash, my stack looks like this:

* thread #1: tid = 0x22cc82, 0x01611411 CoreFoundation`___forwarding___ + 769, queue = 'com.apple.main-thread', stop reason = EXC_BREAKPOINT (code=EXC_I386_BPT, subcode=0x0)
  * frame #0: 0x01611411 CoreFoundation`___forwarding___ + 769
    frame #1: 0x016110ee CoreFoundation`__forwarding_prep_0___ + 14
    frame #2: 0x00b0bc34 UIKit`-[UICollectionViewData _updateItemCounts] + 331
    frame #3: 0x00b0bce1 UIKit`-[UICollectionViewData _validateItemCounts] + 45
    frame #4: 0x00b0dc64 UIKit`-[UICollectionViewData numberOfSections] + 35
    frame #5: 0x00ad86ed UIKit`-[UICollectionView numberOfSections] + 62
    frame #6: 0x00af8614 UIKit`-[UICollectionViewFlowLayout(Internal) _getSizingInfos] + 205
    frame #7: 0x00af948f UIKit`-[UICollectionViewFlowLayout(Internal) _fetchItemsInfo] + 521
    frame #8: 0x00af4fe2 UIKit`-[UICollectionViewFlowLayout prepareLayout] + 157
    frame #9: 0x00b0bd9d UIKit`-[UICollectionViewData _prepareToLoadData] + 76
    frame #10: 0x00b0c5c5 UIKit`-[UICollectionViewData validateLayoutInRect:] + 56
    frame #11: 0x00ad2603 UIKit`-[UICollectionView layoutSubviews] + 173
    frame #12: 0x004f5964 UIKit`-[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 355
    frame #13: 0x013b282b libobjc.A.dylib`-[NSObject performSelector:withObject:] + 70
    frame #14: 0x0438145a QuartzCore`-[CALayer layoutSublayers] + 148
    frame #15: 0x04375244 QuartzCore`CA::Layer::layout_if_needed(CA::Transaction*) + 380
    frame #16: 0x043750b0 QuartzCore`CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 26
    frame #17: 0x042db7fa QuartzCore`CA::Context::commit_transaction(CA::Transaction*) + 294
    frame #18: 0x042dcb85 QuartzCore`CA::Transaction::commit() + 393
    frame #19: 0x0439a5b0 QuartzCore`+[CATransaction flush] + 52
    frame #20: 0x004849bb UIKit`_UIApplicationHandleEventQueue + 13095
    frame #21: 0x015aa77f CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
    frame #22: 0x015aa10b CoreFoundation`__CFRunLoopDoSources0 + 235
    frame #23: 0x015c71ae CoreFoundation`__CFRunLoopRun + 910
    frame #24: 0x015c69d3 CoreFoundation`CFRunLoopRunSpecific + 467
    frame #25: 0x015c67eb CoreFoundation`CFRunLoopRunInMode + 123
    frame #26: 0x02b5c5ee GraphicsServices`GSEventRunModal + 192
    frame #27: 0x02b5c42b GraphicsServices`GSEventRun + 104
    frame #28: 0x00486f9b UIKit`UIApplicationMain + 1225
    frame #29: 0x000026bd Niknik`main(argc=1, argv=0xbfffee74) + 141 at main.m:9

Also, the problem seems to be that the data source is getting deallocated somehow, because when I turned on zombies, I got this:

-[CustomCollectionViewDataSource collectionView:numberOfItemsInSection:]: message sent to deallocated instance 0xa0a8d10

What am I doing wrong?

Это было полезно?

Решение

You are creating the dataSource as a local variable, so once setupCollectionView exits, it goes out of scope and can be deallocated. collectionView will only retain a weak reference so there is nothing to prevent the deallocation.

You should create a property for your dataSource -

@property (strong,nonatomic) CustomCollectionViewDataSource *dataSource  

and then use this in your setup method -

- (void)setupCollectionView:(UICollectionView*)collectionView {
    self.dataSource = [[CustomCollectionViewDataSource alloc] initWithCollectionView: collectionView];
    [collectionView setDataSource: self.dataSource];
    [collectionView setDelegate: self];
    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:kNNReuseIdentifier];
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top