[UITableView _configureCellForDisplay:forIndexPath:]

刚刚在开发的时候碰到这样一个问题,闪退,lldb提示信息:[UITableView _configureCellForDisplay:forIndexPath:],之前遇到过这个问题,忘了怎么解决了,然后又是一堆搜索。结果发现是因为
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
方法返回的cell为nil的问题
参考这里
问题代码如下,这是会发生闪退的代码

- (UITableView *)canJoinTableView
{
    if (!_canJoinTableView) {
        _canJoinTableView = [[UITableView alloc] init];
        [_canJoinTableView registerClass:[WLInvestAssistantUnJoinedCell class] forCellReuseIdentifier:kInvestAssistantCellUnJoined];
        [_canJoinTableView registerClass:[WLInvestAssistantJoinedCell class] forCellReuseIdentifier:kInvestAssistantCellJoined];
        _canJoinTableView.delegate = self;
        _canJoinTableView.dataSource = self;
        _canJoinTableView.backgroundColor = WLColorBgGray_VC;
        _canJoinTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _canJoinTableView;
}

修复后正确代码如下:

- (UITableView *)canJoinTableView
{
    if (!_canJoinTableView) {
        _canJoinTableView = [[UITableView alloc] init];
        _canJoinTableView.delegate = self;
        _canJoinTableView.dataSource = self;
        _canJoinTableView.backgroundColor = WLColorBgGray_VC;
        _canJoinTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_canJoinTableView registerClass:[WLInvestAssistantUnJoinedCell class] forCellReuseIdentifier:kInvestAssistantCellUnJoined];
        [_canJoinTableView registerClass:[WLInvestAssistantJoinedCell class] forCellReuseIdentifier:kInvestAssistantCellJoined];
    }
    return _canJoinTableView;
}

细心的读者可能发现只是调整了设置delegate、dataSource和 registerClass:的位置而已,调整代码顺序之后闪退现象消失,返回的cell不为nil,个人理解是registerClass:的内部实现依赖于设置delegate、dataSource的代理方法,所以必须按照正确顺序实现。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容