Uitableview deleteRowsAtIndexPaths导致的崩溃

Uitableview deleteRowsAtIndexPaths导致的崩溃,uitableview,Uitableview,我有UISwitch,它在两种表视图状态之间切换。问题是快速切换导致崩溃。我认为这是因为删除行需要一些时间(因为动画),如果我添加了一些单元格,并且几乎同时尝试删除它们,那么就会崩溃。我能做什么?我非常想要动画,所以[self.tableView reloadData]不是解决方案 - (void)switchChangedInIndexPath:(NSIndexPath *)indexPath { EBOrderFormCell *cell = (EBOrderFormCell *)[

我有UISwitch,它在两种表视图状态之间切换。问题是快速切换导致崩溃。我认为这是因为删除行需要一些时间(因为动画),如果我添加了一些单元格,并且几乎同时尝试删除它们,那么就会崩溃。我能做什么?我非常想要动画,所以[self.tableView reloadData]不是解决方案

- (void)switchChangedInIndexPath:(NSIndexPath *)indexPath
{
    EBOrderFormCell *cell = (EBOrderFormCell *)[self.tableView cellForRowAtIndexPath:indexPath];
    BOOL on = cell.picker.on;
    if (indexPath.row == EBOrderFormTakeAway) {
        self.takeAway = on;
        [self takeAwayChanged];
    }
}

- (void)takeAwayChanged
{
    NSArray *notTakeAwayCells = [[NSArray alloc] initWithObjects:
                                    [NSIndexPath indexPathForRow:EBOrderFormStreet inSection:0],
                                    [NSIndexPath indexPathForRow:EBOrderFormHouseNumber inSection:0],
                                    [NSIndexPath indexPathForRow:EBOrderFormFlatNumber inSection:0],
                                    nil];
    NSArray *takeAwayCells = [[NSArray alloc] initWithObjects:
                              [NSIndexPath indexPathForRow:EBOrderFormCafe inSection:0],
                              nil];
    [self.tableView beginUpdates];
    if (self.takeAway) {
        [self.tableView insertRowsAtIndexPaths:takeAwayCells withRowAnimation:UITableViewRowAnimationFade];
        [self.tableView deleteRowsAtIndexPaths:notTakeAwayCells withRowAnimation:UITableViewRowAnimationFade];
    } else {
        [self.tableView insertRowsAtIndexPaths:notTakeAwayCells withRowAnimation:UITableViewRowAnimationFade];
        [self.tableView deleteRowsAtIndexPaths:takeAwayCells withRowAnimation:UITableViewRowAnimationFade];
    }
    [self.tableView endUpdates];
}
更新:用代码修复了以下问题:

- (void)takeAwayChanged
{
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
}

通常,您会在动画期间禁用用户交互,但由于您需要一个完成处理程序来知道何时重新打开用户交互(表视图批处理更新不提供此功能),因此您可以尝试将批处理更新发布到块中,以使表视图有机会停止搅动:

    dispatch_async(dispatch_get_main_queue(), ^{
        [self.tableView beginUpdates];
        //...
        [self.tableView endUpdates];
    });
通过这样做,我已经解决了重叠批量更新导致的崩溃问题。但我不得不说,我试图重现你的问题,但没有成功。所以可能还有另一个问题

如果你仍然有麻烦,考虑建立你的表。它为您计算并执行批量更新,非常健壮。针对您的问题,请尝试运行。打开和关闭“声音”开关可以快速隐藏和显示一行,并且不会崩溃。

“我能做什么?”--做出一些明显的努力来调试您的问题。