UITableView Controller报错解决方法

本文详细介绍了在iOS开发中遇到TableView组件无法解除队列的cell标识符未注册错误的原因及解决方案。通过在viewDidLoad方法中注册cell标识符,确保在使用cellForRowAtIndexPath方法时不会出现此错误。文章还提供了关键代码示例,帮助开发者快速定位和解决相关问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

TableView

报错解决方案与解释

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard' 

未注册Cell,凡5.1版本起的xcode,纯代码写tableview,都要给返回的cell注册,无论是系统的还是自定义的cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
<span style="color:#009900;">    static NSString *CellIdentifier = @"Cell"; 
</span><span style="color:#ff0000;">    [self.tableView registerClass:[</span><span style="color:#3366ff;">UITableViewCell</span><span style="color:#ff0000;"> class] forCellReuseIdentifier:CellIdentifier]; </span>
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 
    if (cell == nil) 
    { 
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
          cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    } 
    // config cell
  ...    
   return cell; 
}

绿色部分代码是标识cell,建议放到viewdidload方法上面;

红色部分代码则是注册cell,建议放到viewdidload方法里面,第一次进入页面就会注册cell 而不用每次返回 cell 去注册.

蓝色部分UITableViewCell是系统自带的cell,如果自定义cell也需要注册,把UITableViewCell改为自定义的cell类名即可;



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值