博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS exception 'NSInternalInconsistencyException', reason: 'UICollectionView dataSource is not set'
阅读量:2432 次
发布时间:2019-05-10

本文共 4854 字,大约阅读时间需要 16 分钟。

错误的完整提示代码:

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
效果图

注意设置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个方法和这个方法很像, 不要选择错了

你可能感兴趣的文章
Ionic-与时间有关的故事-localecompare()
查看>>
Logback-spring.xml日志配置
查看>>
[Vue warn]: Property or method "name" is not defined on the instance but referenced during render
查看>>
ts:json串转换成数组
查看>>
String、StringBuffer和StringBuilder的区别
查看>>
java——职责链模式
查看>>
java_选择类排序——简单选择排序
查看>>
java_中介者模式
查看>>
java_备忘录模式
查看>>
多线程——背景了解
查看>>
power designer使Comment与Name相同.txt
查看>>
学习Spring 开发指南------基础语义
查看>>
IE下的图片空隙间距BUG和解决办法
查看>>
[pb]从excel导入数据到datawindow
查看>>
CSS Padding in Outlook 2007 and 2010
查看>>
有关内存的思考题
查看>>
What is the difference between gross sales and revenue?
查看>>
Dreamweaver默认打开后缀名为ftl的文件时
查看>>
LNMP一键安装
查看>>
几个分析函数的比较
查看>>