这个UITableView删除代码有什么问题吗?

时间:2011-05-28 19:40:47

标签: ios uitableview

我从以下代码收到以下错误,请您告诉我我的问题是什么。

  

***由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'无效更新:第0节中的行数无效。更新后的现有部分中包含的行数(2)必须等于更新前该部分中包含的行数(2),加上或减去从该部分插入或删除的行数(插入0,删除1)。'

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        id object = [[[self cdArray] objectAtIndex:indexPath.row] retain];
        [[self cdArray] removeObject:object];
        [[self managedObjectContext] deleteObject:object];
        [self refreshArray];
        NSError *error = nil;
        if (![[self managedObjectContext] save:&error]) {
            NSLog(@"\n%s\n%@", __PRETTY_FUNCTION__, [error description]);
        }
        [[self tableView] deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop];
    }
}

- (void) refreshArray {
    if ([self cdArray] == nil) {
        [self setCdArray:[[NSMutableArray alloc] init]];
    }
    [cdArray removeAllObjects];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSError *error = nil;
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Image" 
                                              inManagedObjectContext:[self managedObjectContext]];
    [fetchRequest setEntity:entity];
    NSArray *fetchedObjects = [[self managedObjectContext] executeFetchRequest:fetchRequest error:&error];    
    for (CDImage *image in fetchedObjects) {
        [cdArray addObject:image];
    }
    [[self tableView] reloadData];
    [fetchRequest release];
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}

1 个答案:

答案 0 :(得分:0)

你不应该打电话

[self refreshArray];

直到您在此处提交删除后

NSError *error = nil;
if (![[self managedObjectContext] save:&error]) {
    NSLog(@"\n%s\n%@", __PRETTY_FUNCTION__, [error description]);
}

你应该换行。