【问题标题】:exception while deleting cell删除单元格时出现异常
【发布时间】:2011-07-29 22:12:22
【问题描述】:

我正在尝试从我的表格视图中删除一个单元格,但它显示异常:

由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“*** -[RootViewController aBook]:无法识别的选择器发送到实例 0x3d217a0”

这是我的代码:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { NSLog(@"将值移交给对象"); Book *aBook = [appDelegate.books objectAtIndex:indexPath.row]; NSLog(@"删除"); [[self aBook] removeObjectAtIndex:[indexPath row]]; NSLog(@"删除行"); // 动画删除 NSArray *indexPaths = [NSArray arrayWithObject:indexPath]; [[self tableView] deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade]; }

控件在打印“removing”字样后在第 8 行显示异常。还警告“根视图控制器没有响应 aBook” 帮助!!

【问题讨论】:

    标签: iphone cocoa-touch


    【解决方案1】:

    你为什么叫[self aBook]?这是本地对象!试试 [aBook removeObjectAtIndex: ]

    【讨论】:

    • 当我使用 aBook removeObjectAtIndex:[indexPath row]]; 时仍然给出相同的异常;
    【解决方案2】:

    运行时已经很好地解释了问题所在:您正在向 RootViewController 类型的对象(实现发布代码的类)发送 aBook 消息。

    所以,[self aBook] 无效。

    另外,接受更多答案。

    【讨论】:

    • 好的!我尝试了代码行:aBook removeObjectAtIndex:[indexPath row]];其他开发人员建议,但仍然报同样的错误!
    • 它还会发出警告“图书可能无法响应 removeobjectatindex”。一定是我犯了错误,但我无法追查。
    【解决方案3】:

    也许你的意思

    [aBook removeObjectAtIndex:[indexPath row]];
    

    【讨论】:

      猜你喜欢
      • 2019-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多