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

原创 2016年08月31日 16:49:48

错误的完整提示代码:

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


ViewController.m

#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之NSMutableDictionary导致程序崩溃:'NSInternalInconsistencyException'

使用NSMutableDictionary时,如果操作不当,有可能会引起程序崩溃。示例代码: NSString *result = @"{\"username\”:\”aaa\”,\"phone\"...

iOS调用reloadRowsAtIndexPaths Crash报异常NSInternalInconsistencyException

在更新 TableView 的时候调用[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath inde...

我是如何成为一名python大咖的?

人生苦短,都说必须python,那么我分享下我是如何从小白成为Python资深开发者的吧。2014年我大学刚毕业..

iOS6新特征:UICollectionView介绍

1.1. Collection View 全家福: UICollectionView, UITableView, NSCollectionView n   不直接等效于NSCollectionV...
  • eqera
  • eqera
  • 2012-11-01 07:51
  • 149015

错误:Assertion failure in -[UICollectionView _createPreparedSupplementaryViewForElementOfKind:atIndexP

错误在使用UICollectionView的时候, 设置collectionView的header或者footer后, 报了以下错误:

UICollectionView reloadData 遇上 layoutAttributesForSupplementaryElementOfKind “crash”

crash log: 2015-12-20 15:48:46.076 iosclient[1993:46171] *** Terminating app due to uncaugh...

iOS UICollectionView简单使用

NS_CLASS_AVAILABLE_IOS(6_0) @interface UICollectionView : UIScrollView

nested exception is java.io.FileNotFoundException: class path resource [spring/spring-datasource-mog

spring单元测试时发现的问题: org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsi...

IOS 10 Xcode8 dyld: Library not loaded: Reason: image not found

dyld: Library not loaded: /System/Library/Frameworks/UserNotifications.framework/UserNotifications ...

Could not open JPA EntityManager for transaction; nested exception is javax.pers

错误栈: org.zkoss.zk.ui.UiException: Could not open JPA EntityManager for transaction; nested exceptio...

使用Hibernate出现nested exception is org.hibernate.MappingException: entity class not found:问题

启动tomcat 后出现如下错误: 严重: Exception sending context initialized event to listener instance of class o...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)