1

嗨,以下功能对我不起作用,我不明白为什么....

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{    
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        // Delete the row from the data source
        NSString *selectedCategory = [self.daten objectAtIndex:indexPath.row];
        [self.daten removeObjectAtIndex:indexPath.row]; //we assume, that mySourceArray is a NSMutableArray we use as our data source
        [self.tableView beginUpdates];

        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

        [self.tableView endUpdates];

        // ...
    }
}

我没有任何部分......我总是得到那个例外:

由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无效更新:第 0 节中的行数无效。更新后现有节中包含的行数 (19) 必须等于该节中包含的行数更新前的节 (19),加上或减去从该节插入或删除的行数(0 插入,1 删除),加上或减去移入或移出该节的行数(0 移入,0 移动出去)。' *First throw call stack: (0x342692a3 0x33a3b97f 0x3426915d 0x3922e2af 0x36dd5b83 0x5453d 0x36f6b5d9 0x36e8c0ad 0x36e8c05f 0x36e8c03d 0x36e8b8f3 0x36e8c0ad 0x36e8c05f 0x36e8c03d 0x36e8b8f3 0x36e8bde9 0x36db45f9 0x36da1809 0x36da1123 0x34f195a3 0x34f191d3 0x3423e173 0x3423e117 0x3423cf99 0x341afebd 0x341afd49 0x34f182eb 0x36df5301 0x24343 0x38e34b20) libc++abi.dylib: terminate called throwing an exception

4

1 回答 1

2

您需要更新numberOfRowsInSection:方法中的行数。如我所见,您需要以[self.daten count]这种方法返回您的。

正如其所说,系统在更新/删除之前/之后检查此数字以保持数据完整性。

你总是有一个部分:)

像这样实现它:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.daten count];
}

另外,请记住,您需要同时设置UITableViewDelegateUITableViewDataSourceself

好的,问题是:NSMutableArrayremoveObjectAtIndex:方法没有调整它的大小。所以你必须自己处理。创建具有正确容量的新数组并将其余元素移至其中。你可以谷歌how to resize NSMutableArray

于 2012-10-04T14:19:28.827 回答