iOS exception 'NSInternalInconsistencyException', reason: 'UICollectionView dataSource is not set'

错误的完整提示代码:

2016-08-31 16:13:34.189 xxxx[1424:25534] *** Assertion failure in -[UICollectionView _createPreparedSupplementaryViewForElementOfKind:atIndexPath:withLayoutAttributes:applyAttributes:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3512.60.7/UICollectionView.m:1599


2016-08-31 16:13:34.278 xxxx[1424:25534] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UICollectionView dataSource is not set'


出现这个错误的情况是我设置了collectionView中每个section的header

而且错误并不是想错误描述的UICollectionView dataSource is not set没有设置数据源

先说说如何设置collectionView的header

3个文件需要写代码, HeaderView.h(设置header时用的view), HeaderView.m, ViewController.m

直接上代码

HeaderView.h

#import 

@interface HeaderView : UICollectionReusableView

@property(nonatomic, strong)UILabel *titleLabel;

@end

HeaderView.m

#import "HeaderView.h"

@implementation HeaderView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.text = @"这里是section的header";
        _titleLabel.backgroundColor = [UIColor greenColor];
        [self addSubview:_titleLabel];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    _titleLabel.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
}
    

@end


ViewController.m

#import "ViewController.h"
#import "HeaderView.h"

@interface ViewController ()

@property(nonatomic, strong)UICollectionView *collectionView;

@end

@implementation ViewController

- (void)loadView
{
    [super loadView];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(80, 30);
    flowLayout.minimumLineSpacing = 10;
    flowLayout.minimumInteritemSpacing = 10;
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    // 注意这句话: 当collectionView不是根视图的时候, 存在返回按钮, 返回前一页, 此时设置header的尺寸这样写返回前一页后会crash, 要用代理方法设置高度
//    [flowLayout setHeaderReferenceSize:CGSizeMake(self.view.frame.size.width, 30)];
    
    _collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:flowLayout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"identifier"];
    [_collectionView registerClass:[HeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerIdentifier"];
    [self.view addSubview:_collectionView];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 4;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableView = nil;
    if (kind == UICollectionElementKindSectionHeader)
    {
        HeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerIdentifier" forIndexPath:indexPath];
        reusableView = headerView;
    }
    return reusableView;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 20;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"identifier" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor redColor];
    return cell;
}

// 代理方法设置header的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(self.view.frame.size.width, 30);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

效果图

iOS exception 'NSInternalInconsistencyException', reason: 'UICollectionView dataSource is not set'_第1张图片


注意设置section的header尺寸的这句话

[flowLayout setHeaderReferenceSize:CGSizeMake(self.view.frame.size.width, 30)];

平时我们看demo或者测试的时候, 就一个视图, 直接设置这个就可以了而且不会看出什么问题, 在我的项目中, 会在push出来的控制器中有一个collectionView用来筛选, 这个筛选要设置每个section的header-title, 当我pop回前一个视图后就会crash出现文章开头的错误, 经过无数次百度和google之后都没有查到解决办法, 最后经过百般尝试终于找到问题所在, 就是因为用上面的set方法设置了header的尺寸

解决办法:

[flowLayout setHeaderReferenceSize:CGSizeMake(self.view.frame.size.width, 30)]; // 将这句代码删除

在代理方法中设置header的尺寸

// 代理方法设置header的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(self.view.frame.size.width, 30);
}

这里要注意一下, 还有2个方法和这个方法很像, 不要选择错了




你可能感兴趣的:(iOS exception 'NSInternalInconsistencyException', reason: 'UICollectionView dataSource is not set')