美文网首页
[UITableView _configureCellForDi

[UITableView _configureCellForDi

作者: C_HPY | 来源:发表于2016-12-07 13:23 被阅读34次

    刚刚在开发的时候碰到这样一个问题,闪退,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的代理方法,所以必须按照正确顺序实现。

    相关文章

      网友评论

          本文标题:[UITableView _configureCellForDi

          本文链接:https://www.haomeiwen.com/subject/rovdmttx.html