ios - 应用程序在同一个 'UITableViewCell' 中出现两个自定义 'UITableView' 崩溃

标签 ios objective-c uitableview unrecognized-selector

我有一个“UITableView”,应该加载一个自定义“UITableViewCell”。当用户触摸其中一个单元格时,触摸的单元格将替换为另一个自定义单元格,并带有额外的信息标签。

但是,当用户触摸单元格时,应用程序会崩溃。它引发了以下异常:

'NSInvalidArgumentException', reason: '-[simplefiedCell cellInfo]: unrecognized selector sent to instance 0x7fb5b2f3d950'

(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 的代码在哪里:

if(indexPath.row == self.selectedCell) {

    regularCell *cell = (regularCell *)[tableView dequeueReusableCellWithIdentifier:cellID1];

    [cell.cellInfo setText:@"Some temporally text to test this code"];


    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"regularCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    return cell;

}else{

    simplefiedCell *cell = (simplefiedCell *)[tableView dequeueReusableCellWithIdentifier:cellID2];

    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"simplefiedCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    switch (indexPath.row) {
        case 0:
            [cell.cellTopic setText:@"Text1"];
            break;
        case 1:
            [cell.cellTopic setText:@"Text2"];
            break;
        case 2:
            [cell.cellTopic setText:@"Text3"];
            break;
        case 3:
            [cell.cellTopic setText:@"Text4"];
            break;
        case 4:
            [cell.cellTopic setText:@"Text5"];
            break;
        case 5:
            [cell.cellTopic setText:@"Text6"];
            break;
        default:
            break;
    }

    return cell;
}

我在 (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 中使用的简单代码:

[tableView deselectRowAtIndexPath:indexPath animated:YES];

if (indexPath.row == self.selectedCell)
    self.selectedCell = -1;
else
    self.selectedCell = indexPath.row;

[self.tableView reloadData]; 

有人知道这是怎么回事吗?感谢任何帮助!

最佳答案

乍一看,我怀疑问题不在您上面粘贴的代码中。可能性是:

  • 您不小心为 cellID1 注册了 simpleCell NIB,当您需要常规单元时,却得到了简化的单元类别。
  • cellID1和cellID2的值相同,导致后来的注册获胜。
  • 您在常规Cell XIB中指定了错误的自定义类,并且实际上正在实例化简化的Cell类。

如果这三个都不是这种情况,那么我建议搜索简化的单元类的名称 - 首先在 XIB xml 文件中,然后在代码中 - 无论你在哪里找到它,问问自己它是否真的应该是代替那个地方的普通类。大概其中之一应该是。

关于ios - 应用程序在同一个 'UITableViewCell' 中出现两个自定义 'UITableView' 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32162381/

相关文章:

ios - 使用 JSON 对象填充 Swift 中的 TableView

ios - 如何使 Http 请求在 TableView 单元格之前按时加载

objective-c - Objective C 自定义运算符函数

ios - Xamarin - UITableView 重新加载数组数据

ios - 当 for 循环从 NSArray 到达 NSNotFound 时如何中断

objective-c - 尝试使用 oauth 和 gdata objective-c api 列出谷歌文档

ios - UITableview 默认单元格中的自定义分隔符显示意外行为

ios - 如何在一个 UITableView 中维护不同布局的单元格

ios - Swift 3 DispatchGroup单次成功DispatchWorkItem

ios - 通过 Segue 将数据传递给新的 ViewController