調(diào)用setCollectionViewLayout 崩潰

很詭異的crash
發(fā)生在Xcode13 Version 11.1 (11A1027)

用法

- (void)setupLayoutAndCollectionView{
    CHTCollectionViewWaterfallLayout* flowLayout = [[CHTCollectionViewWaterfallLayout alloc] init];
    if (self.hasFollow == NO) {
        flowLayout.columnCount = 1;
        flowLayout.minimumInteritemSpacing = 0;
        
        [self.view addSubview:self.attensionHeaderView];
        [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(UIEdgeInsetsMake(self.attensionHeaderView.height, 0, 0, 0));
        }];
    }else {
        flowLayout.columnCount = kFeedColumnCount;
        flowLayout.sectionInset = kFeedSecitonInset;
        flowLayout.minimumColumnSpacing = kFeedColumnSpacing;
        flowLayout.minimumInteritemSpacing = kFeedInteritemSpacing;
        
        [self.attensionHeaderView removeFromSuperview];
        [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(UIEdgeInsetsZero);
        }];
    }
    
    [self.collectionView setCollectionViewLayout:flowLayout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([HomeCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:NSStringFromClass([HomeCollectionViewCell class])];
    [self.collectionView registerClass:[AttensionCell class] forCellWithReuseIdentifier:NSStringFromClass([AttensionCell class])];
}

當(dāng)self.hasFollow 變化時(shí),重設(shè)flowLayout,然后崩潰??

(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x8)
    frame #0: 0x000000018b410a20 UIKit`-[UICollectionViewData layoutAttributesForItemAtIndexPath:] + 248
    frame #1: 0x000000018b4ea588 UIKit`-[UICollectionView _setCollectionViewLayout:animated:isInteractive:completion:animator:] + 2284
    frame #2: 0x000000018b4e9c84 UIKit`-[UICollectionView _setCollectionViewLayout:animated:isInteractive:completion:] + 84
    frame #3: 0x000000018b4e9c10 UIKit`-[UICollectionView setCollectionViewLayout:] + 388
  * frame #4: 0x0000000100762a68 deepLeaper`-[AttensionListController setupLayoutAndCollectionView](self=0x00000001557d7e00, _cmd="setupLayoutAndCollectionView") at AttensionListController.m:107:5
    frame #5: 0x0000000100762538 deepLeaper`__57-[AttensionListController requestDataComplete:errorInfo:]_block_invoke(.block_descriptor=0x00000001571f7640, hasFollow=YES) at AttensionListController.m:64:9
    frame #6: 0x00000001006fcc7c deepLeaper`__65+[CHRequestManager(.block_descriptor=0x00000001557b1830, responseObject=4 key/value pairs) requestHasFollowOnSucceeded:onError:]_block_invoke at CHRequestManager+Content.m:72:13
    frame #7: 0x000000010078e730 deepLeaper`__78+[CHRequestManager requestWithRequest:path:method:parameters:success:failure:]_block_invoke(.block_descriptor=0x00000001571f5690, request=0x00000001571882e0) at CHRequestManager.m:61:17
    frame #8: 0x000000010082efc4 deepLeaper`__61-[CHBaseRequest startWithCompletionBlockWithSuccess:failure:]_block_invoke(.block_descriptor=0x00000001557eed30, request=0x00000001571882e0) at CHBaseRequest.m:177:13
    frame #9: 0x0000000100a84bf0 deepLeaper`__48-[YTKNetworkAgent requestDidSucceedWithRequest:]_block_invoke(.block_descriptor=0x00000001571f3590) at YTKNetworkAgent.m:372:13
    frame #10: 0x0000000103ee51dc libdispatch.dylib`_dispatch_call_block_and_release + 24
    frame #11: 0x0000000103ee519c libdispatch.dylib`_dispatch_client_callout + 16
    frame #12: 0x0000000103ee9d2c libdispatch.dylib`_dispatch_main_queue_callback_4CF + 1180
    frame #13: 0x0000000181653070 CoreFoundation`__CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 12
    frame #14: 0x0000000181650bc8 CoreFoundation`__CFRunLoopRun + 2272
    frame #15: 0x0000000181570da8 CoreFoundation`CFRunLoopRunSpecific + 552
    frame #16: 0x0000000183556020 GraphicsServices`GSEventRunModal + 100
    frame #17: 0x000000018b590758 UIKit`UIApplicationMain + 236
    frame #18: 0x00000001007d1278 deepLeaper`main(argc=1, argv=0x000000016f8d37a8) at main.m:14:16
    frame #19: 0x0000000181001fc0 libdyld.dylib`start + 4

懷疑可能是數(shù)據(jù)清了,cell沒清掉時(shí) 去設(shè)置另一種flowLayout 會(huì)導(dǎo)致崩潰
后來在調(diào)用之前,reloadData一下就好了

      if (self.hasFollow != hasFollow) {
            self.dataArray = @[]; //清除
            [self.collectionView reloadData]; //加上就不crash了
        }
        self.hasFollow = hasFollow;
        [self setupLayoutAndCollectionView];
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容