UITableView错误failed to obtain a cell from its dataSource的原因和解决办法

在使用UITableView时遇到错误:failed to obtain a cell from its dataSource。原因是导航控制器导致UITableView对象状态异常,使得第二次进入时无法正确加载。解决方法是在viewDidLoad中添加一个BOOL变量hasLoad并初始化为NO,确保每次创建对象时都能重新从NIB加载。
摘要由CSDN通过智能技术生成

调试报错:

MultView.temp_caseinsensitive_rename[2995:1888395] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView (<UITableView: 0x150102400; frame = (0 0; 600 622); clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x14fde21c0>; layer = <CALayer: 0x14fe891a0>; contentOffset: {0, 0}; contentSize: {600, 678}>) failed to obtain a cell from its dataSource (<ThirdViewController: 0x14feb9560>)'

原因:本人初学者,网上看了些代码,拿来自己用。结果单独的tab bar运行没问题,我自己加了navigation controller 出现问题了。第一次进入tab bar是没问题的,返回navigation 后,再进入tab bar 后,UITableView就报错了。仔细查找 果然是

-(UITableViewCell * ) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static BOOL nibsRegistered = NO;
    static NSString *CustomCellIdentifier = @"tableViewCell";
    

    if (!nibsRegistered) {
        UINib *nib = [UINib nibWithNibName:@"TableViewCell" bundle:nil];
        [tableView registerNib:nib forCellReuseIdentifier:CustomCellIdentifier];
        nibsRegistered = YES;
    }
    NSLog(@"cellForRowAtIndexPath %li",indexPath.section);
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
    switch (indexPath.section%3) {
        case 0:
            [cell.imageView setImage:[UIImage imageNamed:@"mw-r1.jpg"]];
            break;
        case 1:
            [cell.imageView setImage:[UIImage imageNamed:@"mw-r2.jpg"]];
            break;
        case 2:
            [cell.imageView setImage:[UIImage imageNamed:@"mw-r3.jpg"]];
            break;

        default:
            break;
    }
    if(cell == nil)
    {
        NSLog(@"[Error]cell is nil");
    }
    NSLog(@"cellForRowAtIndexPath Finished Creation %li",indexPath.section);
    return cell;
}

cell 对象是空的,然后 仔细排查,原来是static BOOL nibsRegistered 的错。

对象创建后第一次调用,类变量就变成YES了,然后重新创建对象时就无法 nibWithNibName 了,所以无法通过

[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier] 返回实例对象。

解决办法:

新增一个类变量 BOOL hasLoad ,在 (void)viewDidLoad  函数中声明为 self.hasLoad = NO; 然后改一下,这样每次创建对象就会重新获取NIB了。

-(UITableViewCell * ) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *CustomCellIdentifier = @"tableViewCell";
    

    if (!hasLoad) {
        NSLog(@"Load UINib");
        UINib *nib = [UINib nibWithNibName:@"TableViewCell" bundle:nil];
        [tableView registerNib:nib forCellReuseIdentifier:CustomCellIdentifier];
        hasLoad = YES;
    }
    NSLog(@"cellForRowAtIndexPath %li",indexPath.section);
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
    switch (indexPath.section%3) {
        case 0:
            [cell.imageView setImage:[UIImage imageNamed:@"mw-r1.jpg"]];
            break;
        case 1:
            [cell.imageView setImage:[UIImage imageNamed:@"mw-r2.jpg"]];
            break;
        case 2:
            [cell.imageView setImage:[UIImage imageNamed:@"mw-r3.jpg"]];
            break;

        default:
            break;
    }
    if(cell == nil)
    {
        NSLog(@"[Error]cell is nil");
    }
    NSLog(@"cellForRowAtIndexPath Finished Creation %li",indexPath.section);
    return cell;
}


纯粹自娱自乐,为自己码农生涯做点点记号。。。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值