美文网首页iOS
调用setCollectionViewLayout 崩溃

调用setCollectionViewLayout 崩溃

作者: wustzhy | 来源:发表于2019-11-27 14:02 被阅读0次

很诡异的crash
发生在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])];
}

self.hasFollow 变化时,重设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

怀疑可能是数据清了,cell没清掉时 去设置另一种flowLayout 会导致崩溃
后来在调用之前,reloadData一下就好了

      if (self.hasFollow != hasFollow) {
            self.dataArray = @[]; //清除
            [self.collectionView reloadData]; //加上就不crash了
        }
        self.hasFollow = hasFollow;
        [self setupLayoutAndCollectionView];

相关文章

网友评论

    本文标题:调用setCollectionViewLayout 崩溃

    本文链接:https://www.haomeiwen.com/subject/frspwctx.html