【问题标题】:App crashs with SIGABRT error when trying to delete a row in tableView尝试删除 tableView 中的行时,应用程序因 SIGABRT 错误而崩溃
【发布时间】:2012-03-09 16:39:54
【问题描述】:

当用户尝试从 UITableView 中删除一行时,我的应用程序崩溃,并且在调试器中我收到 SIGABRT 错误。

这是我的删除代码:

- (void) tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
forRowAtIndexPath:(NSIndexPath *)indexPath
{
  if (editingStyle == UITableViewCellEditingStyleDelete)
  {
    [[self displayedObjects] removeObjectAtIndex:[indexPath row]];


    //  Animate deletion
    NSArray *indexPaths = [NSArray arrayWithObject:indexPath];
    [[self tableView] deleteRowsAtIndexPaths:indexPaths
                            withRowAnimation:UITableViewRowAnimationFade];
    //[[self tableView] deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]];
  }
}

【问题讨论】:

  • 尝试删除displayedObjects中对象之前的行。
  • 我想我知道你的问题是什么。我会发布答案
  • 谢谢 Raphael Ayres 我尝试了你的想法,但没有成功。

标签: ios uitableview crash sigabrt delete-row


【解决方案1】:

我在另一个问题上得到了帮助。问题是该对象将从数组中删除,但在从表视图中删除该行之前,再次调用 numberOfRowsInSection,然后再次从磁盘加载数组并给出删除前的数字,因为我的问题加载数组。我在 numberOfRowsInSection 处设置了一个断点,现在一切都清楚了。

【讨论】:

    【解决方案2】:

    您的[self displayedObjects] 的项目计数是否涵盖[indexPath row]?例如,您的 [self displayedObjects] 只有 5 个项目,但您想要 removeObjectAtIndex:6

    【讨论】:

    • 是的。我可以看到显示对象数组正确删除了正确的对象。只有在尝试删除表格视图行时,我的应用才会崩溃。
    【解决方案3】:

    我比较了我的代码,看看你哪里出错了。

            //Removes From Table
        [favorites removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    

    Favorites 是我的数组。我认为你需要做的是改变这一行:

       [[self displayedObjects] removeObjectAtIndex:[indexPath row]];
    

    首先,将这一行放在这一行之前:

    [[self tableView] deleteRowsAtIndexPaths:indexPaths
                                withRowAnimation:UITableViewRowAnimationFade];
    

    然后将self 更改为表格正在显示的数组的名称。

    还有!非常重要

    确保表格显示的数组是NSMutableArray 而不是NSArray

    使用 NSArray,您无法更改(添加或删除)值。但是,您可以使用NSMutableArray,因此请确保您没有显示NSArray

    【讨论】:

    • 我认为您所说的是 Raphael Ayres 在上面的评论中所说的:在从 tableview 从中获取数据的数组中删除对象之前尝试删除 tableview 行。我试过了,但它仍然崩溃。
    • 你确定数组是可变的吗?
    • 是的。它是可变的。顺便说一句,我正在对其进行序列化,然后对其进行反序列化,这给出了一个 NSArray,我制作了一个 NSMutableArray 副本。我希望这不会造成任何问题。我看到数组正确删除了一个对象。我认为是行删除有问题。
    • 尝试完全复制我的代码并用它替换您的代码(注释掉您的代码),然后将“收藏夹”更改为表格显示的数组名称。评论我们您对该方法的整个部分并将我的粘贴在那里。试试看,不知道会不会成功
    • 感谢您的帮助。仍然给出 SIGABRT 错误。这是新代码- (void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { //Removes From Table [_displayedObjects removeObjectAtIndex:indexPath.row]; [tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft]; } }
    【解决方案4】:

    如果我没记错的话,你调用的是这段代码:

     [[self tableView] deleteRowsAtIndexPaths:indexPaths
                                withRowAnimation:UITableViewRowAnimationFade];
    

    来自已提交的删除事件。我相信您需要做的就是从“tableView:commitEditingStyle:forRowAtIndexPath:”方法更新您的后备存储。删除已提交。之后您可能还需要刷新表格视图,同时确保将表格视图标记为允许更新。

    尝试注释掉所有这些代码并告诉我们会发生什么:

    //  Animate deletion
    NSArray *indexPaths = [NSArray arrayWithObject:indexPath];
    [[self tableView] deleteRowsAtIndexPaths:indexPaths
                            withRowAnimation:UITableViewRowAnimationFade];
    

    【讨论】:

    • 关于更新我的后备存储,我认为这就是我正在做的事情。我在这一行中更新了名为displayedObjects 的数组:[[self displayedObjects] removeObjectAtIndex:[indexPath row]];,它正在正常工作,从数组中删除正确的对象。我做了你的建议并评论了行删除。该应用程序不会崩溃,但它不会删除该行,直到我转到另一个视图并返回查看更改。我绑重新加载表,但这也没有帮助。正是这条线导致了崩溃,但我还能如何删除表格行?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-02
    • 1970-01-01
    • 2015-01-20
    • 1970-01-01
    • 1970-01-01
    • 2022-07-26
    • 2014-03-02
    相关资源
    最近更新 更多