tableview的cellForRow方法崩溃

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        [_tableView registerClass:[GraphicsTableViewCell class] forCellReuseIdentifier:kQuotaDetailVc_Graphics_Cell];
        GraphicsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kQuotaDetailVc_Graphics_Cell forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor whiteColor];
        cell.delegate = self;
        cell.cellFrame = _cellFrameArray[indexPath.section];
        cell.graphicsStyle = self.graphicsStyle;
        return cell;
    } else if (indexPath.section == 1) {
        [_tableView registerClass:[QuotaDetailTableViewCell class] forCellReuseIdentifier:kQuotaDetailVc_CellDetail_Cell];
        QuotaDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kQuotaDetailVc_CellDetail_Cell forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor whiteColor];
        cell.cellFrame = _cellFrameArray[indexPath.section];
        return cell;
    } else  {
        [_tableView registerClass:[QuotaCompareTableViewCell class] forCellReuseIdentifier:kQuotaDetailVc_Compare_Cell];
        QuotaCompareTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kQuotaDetailVc_Compare_Cell forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.cellFrame = _cellFrameArray[indexPath.section];
        cell.backgroundColor = [UIColor whiteColor];
        return cell;
    }
}
{
    CGRect frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight - TabBarHeight - ViewCtrlTopBarHeight);
    _tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStyleGrouped];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.scrollEnabled = NO;
    _tableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_tableView];
}

上面的是cellForRow里面的写法,
下面是创建tableview的时候写法,
我这里崩溃的日志是

*** Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:], /SourceCache/UIKit/UIKit-3318.93/UITableView.m:7344

我就没理解怎么就奔溃在tableview的cellForRow方法里面了,cell明明都返回了,

阅读 6.2k
4 个回答

[tableView dequeueReusableCellWithIdentifier:......] 这三个调用,如果有返回空值,程序就会crash。

新手上路,请多包涵

个人认为是cell没有提前注册的原因吧~

这种问题大部分的原因是- (UITableViewCell ) tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 返回的cell为nil. 结合你代码的情况是某个cell的注册没有成功。你可以打断点测试下是具体哪个cell的注册出现问题了。

新手上路,请多包涵

...有一种情况
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
如果在此之前曾经设置过tableView的footerView之类的代码。。。
在iOS8以下的系统上,这句,就会,crash掉

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题