-1

使用 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];
    }
}
4

2 回答 2

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];
   }
}

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

于 2013-05-25T19:56:38.403 回答
0

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

解决方案:由于滑动手势无需点击编辑/完成按钮即可进入编辑模式,我需要以另一种方式进行调整。我通过使用:

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

并在这里进行调整:

- (void)tableView:(UITableView*)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath{
[self makeAdjustments];
}
于 2013-05-26T15:52:06.560 回答