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

调试报错:

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;
}


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


©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页