iOS exception 'NSInternalInconsistencyException', reason: 'UICollectionView dataSource is not set'
来源:互联网 发布:阿里云学生机是什么 编辑:程序博客网 时间:2024/06/05 15:23
错误的完整提示代码:
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 <UIKit/UIKit.h>@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
#import "ViewController.h"#import "HeaderView.h"@interface ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>@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个方法和这个方法很像, 不要选择错了
- iOS exception 'NSInternalInconsistencyException', reason: 'UICollectionView dataSource is not set'
- *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not
- exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle解决办法
- iOS Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to
- 'NSInternalInconsistencyException', reason: 'UICollectionView received layout attributes for a ...
- iOS 用StackView的第三方FDStackView,有时会出现crash,'NSInternalInconsistencyException', reason: 'Multiplier is
- *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could no
- Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFArra
- uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of ro
- * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '* -[NSCFArr
- Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UITableView
- *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid
- Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIViewContr
- 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: !stayUp || CLClientIs
- ***Terminatingappduetouncaughtexception'NSInternalInconsistencyException', reason: 'CouldnotloadNIBi
- 'The NIB data is invalid.' foruncaught exception 'NSInternalInconsistencyException'
- erminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequ
- Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key
- hdu5387 钟表指针之间夹角
- Android状态栏微技巧,带你真正理解沉浸式模式
- iOS--UIWebView清除缓存和Cookie
- tcpdump抓包示例
- Hash小结
- iOS exception 'NSInternalInconsistencyException', reason: 'UICollectionView dataSource is not set'
- Libuv中文文档网络接口
- 使用 libevent 和 libev 提高网络应用性能
- RepositoryRestHandlerMapping在springboot中无法注入
- Linux命令练习第二关(6)
- Android应用程序权限
- Java8-Stream collect
- NS3系列—8———NS3编译运行
- 驰骋工作流引擎表单设计器-级联下拉框