删除表格单元格会导致崩溃

时间:2013-05-25 19:53:27

标签: ios objective-c uitableview

当使用deleteRowsAtIndexPaths删除表格单元格时,如果我尝试使用滑动手势删除单元格以进入编辑模式然后点击删除,则会出现崩溃。如果我通过切换编辑/完成按钮进入编辑模式然后删除单元格,表格会正常更新而不会出现问题。

我也尝试过使用[table beginUpdates]& [table endUpdates]也没有成功。 此外,[table reloadData]也适用于任何一种情况 另外你可以看到我在使用编辑/完成按钮进入编辑模式时尝试了[table reloadSections:[NSIndexSet indexSetWithIndex: indexPath.section] withRowAnimation: UITableViewRowAnimationFade];同样的崩溃。

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{
    if (editingStyle == UITableViewCellEditingStyleDelete) 
    {
        NSMutableArray *temp = nil;
        switch (indexPath.section)
        {
            case 0:
            {
                temp = self.entireSavedArray;
                break;
            }
            case 1:
            {
                temp = self.entireSavedArray2014;
                break;
            }
            case 2:
            {
                temp = self.entireSavedArray2013;
                break;
            }
            case 3:
            {
                temp = self.entireSavedArray2012;
                break;
            } 
            case 4:
            {
                temp = self.entireSavedArray2011;
                break;
            }          
            case 5:
            {
                temp = self.entireSavedArray2010;
                break;
            }
            default:
                break;
        }
        [temp removeObjectAtIndex: ((int)indexPath.row * 11)];
        [temp removeObjectAtIndex: ((int)indexPath.row * 11)];
        [temp removeObjectAtIndex: ((int)indexPath.row * 11)];
        [temp removeObjectAtIndex: ((int)indexPath.row * 11)];
        [temp removeObjectAtIndex: ((int)indexPath.row * 11)];
        [temp removeObjectAtIndex: ((int)indexPath.row * 11)];
        [temp removeObjectAtIndex: ((int)indexPath.row * 11)];
        [temp removeObjectAtIndex: ((int)indexPath.row * 11)];
        [temp removeObjectAtIndex: ((int)indexPath.row * 11)];
        [temp removeObjectAtIndex: ((int)indexPath.row * 11)];
        [temp removeObjectAtIndex: ((int)indexPath.row * 11)];

        [self.table deleteRowsAtIndexPaths: [NSArray arrayWithObjects: indexPath, nil] withRowAnimation: UITableViewRowAnimationFade];
        //[tableView reloadSections:[NSIndexSet indexSetWithIndex: indexPath.section] withRowAnimation: UITableViewRowAnimationFade];
    }
}

2 个答案:

答案 0 :(得分:0)

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
    // Remove from our mutable array
    [data removeObjectAtIndex:indexPath.row];

    // Remove from table view
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
   }
}

删除行时,使用上面的代码对我有用。

答案 1 :(得分:0)

问题在于滑动手势将表格置于编辑模式而不点击编辑/完成按钮。这通常不会有问题,但是当表格未处于编辑模式时,我将占位符单元格添加到表格中,然后在按下完成按钮时将其添加回来。 (我改变了我的数据模型)当我最初创建应用程序时,我通过点击编辑/完成按钮进行调整来解决这个问题。

解决方案:由于滑动手势进入编辑模式而未点击编辑/完成按钮,我需要以其他方式进行调整。我这样做是通过使用:

- (void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath{
[self makeAdjustments];
}

并在此处进行调整:

- (void)tableView:(UITableView*)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath{
[self makeAdjustments];
}
相关问题