当我尝试在其上调用方法时,为什么取消分配UICollectionViewDataSource?

时间:2014-04-25 00:21:12

标签: ios memory-management uicollectionview

虽然我似乎能够在我的视图控制器中将dataSource设置到UICollectionView就好了:

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

当我记录collectionViewdataSourcecollectionView.dataSource时,它们看起来都与预期完全一样。我的收藏视图中只有一个部分,因此我不会在我的数据源中实现numberOfSectionsInCollectionView

我的集合视图加载后,我的应用程序崩溃了。在有或没有断点异常的情况下,没有相关的崩溃日志。当异常断点打开时,它不会指向我的代码导致问题的位置。崩溃后,我的堆栈看起来像这样:

* 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

此外,问题似乎是数据源以某种方式被解除分配,因为当我打开僵尸时,我得到了这个:

- [CustomCollectionViewDataSource collectionView:numberOfItemsInSection:]:发送到解除分配的实例0xa0a8d10的消息

我做错了什么?

1 个答案:

答案 0 :(得分:6)

您正在将dataSource创建为局部变量,因此一旦setupCollectionView退出,它就会超出范围并可以取消分配。 collectionView只会保留weak引用,因此无法阻止重新分配。

您应该为dataSource创建一个属性 -

@property (strong,nonatomic) CustomCollectionViewDataSource *dataSource  

然后在您的设置方法中使用它 -

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