0

我是 iPhone 的新手,我试图从我的 UITableView 中删除一个单元格,第一次它删除得很好,但第二次它给了我以下错误:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', 
reason: 'Invalid update: invalid number of rows in section 0.  The number of rows 
contained in an existing section after the update (3) must be equal to the number 
of rows contained in that section before the update (3), plus or minus the number 
of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or 
minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

这是我的表格代码:

- (void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
Book_own *temp= (Book_own *)[self.books objectAtIndex:indexPath.row];

if (editingStyle == UITableViewCellEditingStyleDelete) {
    // Delete the row from the data source

    [books removeObjectAtIndex:indexPath.row];

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

    isDeleted = @"true";
    deletedBook_id = temp.bo_id;

    [self viewDidLoad];

}

else if (editingStyle == UITableViewCellEditingStyleInsert) {
    // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}  

}


 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 2;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.

NSDictionary *dictionary = [listOfItems objectAtIndex:section];
NSArray *array = [dictionary objectForKey:@"myBooks"];
return [array count];
}

在 ViewDidLoad 我写了以下代码:

NSDictionary *mbooks = [NSDictionary dictionaryWithObject:books forKey:@"myBooks"];

NSDictionary *mgroups = [NSDictionary dictionaryWithObject:filteredParts forKey:@"myBooks"];
listOfItems = [[NSMutableArray alloc] init];

[listOfItems addObject:mbooks];
[listOfItems addObject:mgroups];

谁能告诉我如何解决这个问题??提前致谢。

4

4 回答 4

1

如果我正确阅读了您的代码,则您的数据源是 listOfItems。您应该从表数据源中删除该行。一般规则是,当您删除或添加项目时,UITableView您必须更新数据源。

[listOfItemsremoveObjectAtIndex:indexPath.row];
于 2012-08-28T17:37:03.770 回答
1

发生的情况是您要么没有删除该项目,要么在您允许多次删除时只删除一个项目。您可以通过检查它是否真的被删除来进行调整,如果没有,则重新加载数据:

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        BOOL success = [self removeFile:indexPath];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        if (!success) [tableView reloadData];
    }
}

然后此方法删除数据源项(它来自我自己的项目,因此应调整名称:

-(BOOL) removeFile:(NSIndexPath *)indexPath{
    // Removes from the datasource and the filesystem
    NSURL *fileURL = [self.dataSource objectAtIndex:indexPath.row];
    NSError *error;
    BOOL success = [[NSFileManager defaultManager] removeItemAtURL:fileURL error:&error];
    if (success) {
        [self.dataSource removeObjectAtIndex:indexPath.row];
    } else {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error" message:[error localizedDescription] delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
        [alert show];
        [alert release];
        [self.dataSource removeObjectAtIndex:indexPath.row];
    }
    return success;
}
于 2012-08-28T17:54:44.210 回答
0

该错误表示删除后应该少 1 行。它失败是因为数据源代码在报告模型的方式上不一致。

看看你如何回答 numberOfRowsInSection。我认为正确的是,首先选择节索引表示的数组,然后回答该数组的计数。

相同的逻辑必须适用于删除。您从中删除的数组需要是 indexPath.section 指示的数组。考虑一下:

- (void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    // the array you manipulate here must be the same array you use to count
    // number of rows, and the same you use to render rowAtIndexPath

    NSDictionary *dictionary = [listOfItems objectAtIndex:indexPath.section];
    NSArray *array = [dictionary objectForKey:@"myBooks"];

    // in order to be edited, it must be mutable
    // you can do that on the fly here, or set it up as mutable
    // let's make it temporarily mutable here:
    NSMutableArray *mutableCopy = [array mutableCopy];

    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [mutableCopy removeObjectAtIndex:indexPath.row];

        // if it's setup mutable, you won't need to replace the immutable copy in the dictionary.
        // but we just made a copy, so we have to replace the original
        [listOfItems replaceObjectAtIndex:indexPath.section withObject:[NSArray arrayWithArray:mutableCopy]];

        // and so on
于 2012-08-28T17:51:06.400 回答
0

一、不要调用[self viewDidLoad];不应手动调用此方法。

我认为您没有将更新方法调用到您的表格视图。这可能会解决您的问题:

[tableView beginUpdates];
[books removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];

编辑:您还需要更仔细地查看您的代码。您正在从 indexPath 的行中直接从数据源数组中删除一条记录,考虑到您的 tableView 有两个部分,这可能会出现问题。

于 2012-08-28T17:44:03.563 回答