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

本文介绍了一个UICollectionView在设置section header时导致应用崩溃的问题,并提供了解决方案。通过调整header尺寸的设置方式,避免了因错误配置而导致的应用终止。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

错误的完整提示代码:

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




### 解决Nacos Server 启动失败问题 当遇到 `Nacos Server did not start because dumpservice bean construction failure : No DataSource set` 错误时,这通常意味着应用程序无法找到或初始化数据源配置[^1]。 #### 配置文件检查 确保在 `application.properties` 或者 `application.yml` 文件中有正确的数据库连接配置。对于 MySQL 数据库而言,应该有如下形式的配置: ```properties spring.datasource.platform=mysql db.num=1 db.url.0=jdbc:mysql://localhost:3306/nacos_config?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true db.user=root db.password=password ``` 如果使用的是 YAML 格式的配置,则应像这样定义: ```yaml spring: datasource: platform: mysql db: num: 1 url: "0": jdbc:mysql://localhost:3306/nacos_config?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true user: root password: password ``` #### 版本兼容性验证 确认所使用的 Nacos 和 Spring Boot 的版本之间是否存在已知的问题。有时特定组合可能会导致此类异常行为发生。查阅官方文档来核实当前环境下的最佳实践建议[^2]。 #### 日志分析 仔细查看完整的堆栈跟踪日志信息,寻找更多关于为什么未能成功创建 `dumpService` Bean 的线索。可能涉及到其他依赖项缺失或者是类路径下存在冲突等问题。 通过以上方法可以有效排查并修复由于未设置数据源而导致的 Nacos Server 启动失败的情况。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值