unrecognized selector sent to instance xxx - iOS

TableView 实例化与数据组合渲染的时候抛出了如下的异常:

unrecognized selector sent to instance xxx

纳闷了半天发现了原因 ... 嗯 ... 又大意了 ...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YHMyBillsModel *model = dataSource[indexPath.row];
    InvoiceCenterCell *cell = [InvoiceCenterCell showInvoiceRecordsListCellWithTableView:tableView];
    cell.model = model; // crash 的地方,但根源不在此
    return cell;
}

unrecognized selector sent to instance,selector 即获取类方法对应方法中的标识 id 编号并取出对应的 SEL,而当前函数指针并未找到该类内存地址中对应方法的地址;
TableView 在 registerClass 后会根据 forCellReuseIdentifier 的 Identifier 标识寻找对应的方法,从而链接相互对应的关系,但此时 TableView 在根据 Identifier 标识进行延伸的时候未能找到对应的方法,因此在 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 方法中 cell.model = model 的时候 gg 了。。。具体原因相信大家看了下面解决办法中的错误 code 就能豁然开朗。

 

解决办法

#pragma mark - ****************************** UI
- (UITableView *)tabView {
    if (!_tabView) {
        UITableView *tabView = [[UITableView alloc] initWithFrame:self.view.bounds
                                                            style:UITableViewStyleGrouped];
        tabView.backgroundColor = [UIColor generateDynamicColor:[UIColor whiteColor] darkColor:[UIColor blackColor]];
        tabView.delegate = self;
        tabView.dataSource = self;
//        [tabView registerClass:[UITableViewCell class]
//        forCellReuseIdentifier:CellIdentifierInvoiceRecordsList]; // 导致 Crash 的根源
        [tabView registerClass:[InvoiceCenterCell class]
        forCellReuseIdentifier:CellIdentifierInvoiceRecordsList]; // 修正后
        _tabView = tabView;
    }
    return _tabView;
}

TableView 实例化的时候是否 registerClass 正确的 Cell class;// 也是本次出现问题的地方

个人感觉还会出现的几种可能,如与上问题不符可以参考助于排查:
调用的对象与实际对象不匹配;
自定义类中的方法与实例方法不匹配;
调用的对象提前被释放掉了;

实例对象中 isa 指针去指向类的对象,而类对象中的 isa 指针则会去指向元类的对象;
isa 指向即:
对象 isa -> 类;
类 isa -> 元类;
元类 isa -> 根元类;
根元类 isa -> 根元类自己
从而形成回路。

super class:
对象 super class 指向对象 super class;
元类 super class 指向 super class 的父类;
根元类 meta root class 指向类 NSObject。


以上便是此次分享的全部内容,希望能对大家有所帮助!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值