*** Assertion failure in -[UICollectionViewData validateLayoutInRect:]

错误描述:

*** 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懒加载里面了,没出现过这个问题,今天突然实例化了一下,出现了这个问题,特此记录一下,希望对别人有所帮助。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,148评论 1 32
  • 最近在写一个类似于京东商城分类的页面,不一样的是,我们在选择一个分类下拉帅心数据之后,还能随时切换到按照销量,价格...
    f29da6214cb5阅读 3,459评论 4 3
  • 今天在写项目的时候遇到了一个以前没有碰到的问题 也是个比较低级的问题,为了以后不忘记在这里简单的记一下: 出现这个...
    我是李文艺阅读 722评论 1 0
  • 1.埋点是做什么的 2.如何进行埋点 3.埋点方案的设计 近期常被问到这个问题,我担心我的答案会将一些天真烂漫的孩...
    lxg阅读 2,031评论 0 1
  • 专业考题类型管理运行工作负责人一般作业考题内容选项A选项B选项C选项D选项E选项F正确答案 变电单选GYSZ本规程...
    小白兔去钓鱼阅读 9,059评论 0 13