美文网首页
*** Assertion failure in -[UICol

*** Assertion failure in -[UICol

作者: BBLv | 来源:发表于2019-01-15 10:41 被阅读0次
    错误描述:

    *** Assertion failure in -[UICollectionViewData validateLayoutInRect:]
    *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UICollectionView received layout attributes for a cell with an index path that does not exist: <NSIndexPath: 0xc000000000200016> {length = 2, path = 0 - 1}'

    问题说明:

    我在.h中定义了两个collectionView和一个UICollectionViewFlowLayout,然后在.m懒加载两个collectionView都应用了UICollectionViewFlowLayout的实例,在执行2个collectionView相继刷新的过程中,出现了崩溃。

    问题原理:

    两个collectionView相继刷新时都会用到UICollectionViewFlowLayout实例来验证矩形布局,此时A(collectionView)持有UICollectionViewFlowLayout实例过程中B(collectionView)也要使用UICollectionViewFlowLayout实例,这时就会出现“B(collectionView)接收到具有不存在的索引路径的单元格的布局属性”(系统给出的解释)

    深度剖析原理:

    实例并初始化a、b、c三个对象,a持有c对象的指针指向c内存,b若在a持有过程中想持有c对象,会出现空指针

    解决方案:

    同时实例化两个UICollectionViewFlowLayout

    代码
    .h
    @interface AddPurchaseorderPopLayerView : UIView< UICollectionViewDelegate, UICollectionViewDataSource>
    @property(nonatomic, strong)UICollectionView *collectionView;
    @property(nonatomic, strong)UICollectionViewFlowLayout *flowLayout;
    @property(nonatomic, strong)UICollectionView *typeCollectionView;
    @property(nonatomic, strong)UICollectionViewFlowLayout *flowLayoutType;   //后来添加的
    @end
    
    .m
    #import "AddPurchaseorderPopLayerView.h"
    
    @implementation AddPurchaseorderPopLayerView
    - (instancetype)initWithFrame:(CGRect)frame{
        if (self = [super initWithFrame:frame]) {
            [self setupView];
        }
        return self;
    }
    
    - (void)setupView{
        [self addSubView];
    }
    
    - (void)addSubView{
        [self addSubview:self.collectionView];
        [self addSubview:self.typeCollectionView];  
    }
    
    - (UICollectionViewFlowLayout *)flowLayout{
        if (!_flowLayout) {
            _flowLayout = [[UICollectionViewFlowLayout alloc] init];
            _flowLayout.itemSize = CGSizeMake(SCREEN_WIDTH/3, 65 * FitHeight);
            _flowLayout.minimumInteritemSpacing = 0;
            _flowLayout.minimumLineSpacing = 0;
            _flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
            _flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
            
        }
        return _flowLayout;
    }
    
    - (UICollectionView *)collectionView{
        if (!_collectionView) {
            _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) collectionViewLayout:self.flowLayout];
            _collectionView.delegate = self;
            _collectionView.dataSource = self;
            _collectionView.pagingEnabled = NO;
            _collectionView.backgroundColor = white_color;
            [_collectionView registerClass:[ProductDetailLadderPriceCollectionViewCell class] forCellWithReuseIdentifier:@"identifier"];
        }
        return _collectionView;
    }
    
    //这个是找到问题后加的
    - (UICollectionViewFlowLayout *)flowLayoutType{
        if (!_flowLayoutType) {
            _flowLayoutType = [[UICollectionViewFlowLayout alloc] init];
            _flowLayoutType.itemSize = CGSizeMake(SCREEN_WIDTH/3, 65 * FitHeight);
            _flowLayoutType.minimumInteritemSpacing = 0;
            _flowLayoutType.minimumLineSpacing = 0;
            _flowLayoutType.scrollDirection = UICollectionViewScrollDirectionHorizontal;
            _flowLayoutType.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
        }
        return _flowLayoutType;
    }
    
    //这里面的flowLayoutType原来是flowLayout
    - (UICollectionView *)typeCollectionView{
        if (!_typeCollectionView) {
            _typeCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) collectionViewLayout:self.flowLayoutType];
            _typeCollectionView.delegate = self;
            _typeCollectionView.dataSource = self;
            _typeCollectionView.pagingEnabled = NO;
            _typeCollectionView.backgroundColor = white_color;
            [_typeCollectionView registerClass:[ProductDetailsUnitCollectionViewCell class] forCellWithReuseIdentifier:@"ProductDetailsUnitCollectionViewCell"];
        }
        return _typeCollectionView;
    } 
    

    原来总是把flowLayout写在collectionView懒加载里面了,没出现过这个问题,今天突然实例化了一下,出现了这个问题,特此记录一下,希望对别人有所帮助。

    相关文章

      网友评论

          本文标题:*** Assertion failure in -[UICol

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