ios - tableview的cellForRow方法崩溃
天蓬老师
天蓬老师 2017-04-17 17:44:40
[iOS讨论组]
- (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明明都返回了,

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

全部回复(4)
巴扎黑

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

迷茫

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

黄舟

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

PHP中文网

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

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号