Uitableview ios6删除导致错误的节中的最后一个单元格

Uitableview ios6删除导致错误的节中的最后一个单元格,uitableview,ios6,edit,Uitableview,Ios6,Edit,删除UITableView节中的最后一行时出现此错误 ***由于未捕获异常“NSInternalInconsistencyException”而终止应用程序,原因:“更新无效:节数无效。”。更新后表视图中包含的节数(2)必须等于更新前表视图中包含的节数(3),加上或减去插入或删除的节数(0插入,0删除)。' 如果数组的计数大于0,“我的表”有3个部分(用户可以自己添加位置)。如果数组等于0,那么表只包含2个部分,我认为问题就出在这里,我就是搞不懂 我用于删除单元格和更新数组的代码是: - (vo

删除UITableView节中的最后一行时出现此错误

***由于未捕获异常“NSInternalInconsistencyException”而终止应用程序,原因:“更新无效:节数无效。”。更新后表视图中包含的节数(2)必须等于更新前表视图中包含的节数(3),加上或减去插入或删除的节数(0插入,0删除)。'

如果数组的计数大于0,“我的表”有3个部分(用户可以自己添加位置)。如果数组等于0,那么表只包含2个部分,我认为问题就出在这里,我就是搞不懂

我用于删除单元格和更新数组的代码是:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell* delCell = [tableView cellForRowAtIndexPath:indexPath];
    delName = delCell.textLabel.text;
    [self deleteLocation];
    [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}

-(void)deleteLocation {


    NSLog(@"Name = %@",delName);

    NSUserDefaults *customLocations = [NSUserDefaults standardUserDefaults];
    NSUserDefaults *sharedPref = [NSUserDefaults standardUserDefaults];


    [customLocations removeObjectForKey:delName];
    [customLocations synchronize];

    [customLoc removeObject:delName];
    saveCustomLoc = [NSArray arrayWithArray:customLoc];

    [sharedPref setObject:saveCustomLoc forKey:@"CustomLocations"];
    [sharedPref synchronize];

    NSLog(@"%@",saveCustomLoc);
    NSLog(@"%lu",(unsigned long)[saveCustomLoc count]);

    [self showAlert];



}

- (void) showAlert {

    UIAlertView *alert = [[UIAlertView alloc]

                          initWithTitle:@"Location Deleted"
                          message:@"The location has been deleted"
                          delegate:nil
                          cancelButtonTitle:@"Ok"
                          otherButtonTitles:nil];

    [alert show];

}
这在最后一个单元之前效果很好

如何设置我的表格的示例如下:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

{
    if ([customLoc count] == 0) {   // If there is no data in the custom locations array the don't include that section
        if (section == 0) {
            return [serverSelection count];

        }
        else {
            return [qServerSelection count];

        }
    }
    else {
        if (section == 0) {
            return [customLoc count];

        }
        else if (section == 1) {
            return [serverSelection count];

        }
        else {
            return [qServerSelection count];
        }
    }
因此,标题/行数/节数等取决于数组的内容。我认为,删除单元格的部分消失了(或者说,当第1部分移动到第0部分时,突然有4个单元格)这一事实导致了问题


有什么办法解决这个问题吗?

在进一步研究之后,我发现(在一些苹果论坛成员的帮助下),当删除某个部分的最后一行时,您还需要明确删除该部分

我还在开头和结尾添加了表更新代码,如下所示:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView beginUpdates];

    UITableViewCell* delCell = [tableView cellForRowAtIndexPath:indexPath];
    delName = delCell.textLabel.text;
    [self deleteLocation];
    if ([customLoc count] == 0) {
        [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationTop];
    }
    else {
    [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }

    [tableView endUpdates];

}

因此,如果我的数组为0(实际上最后一行正在被删除),则节本身将被删除。

允许有0行的节。您的问题是由您自己的代码不一致引起的。如果您的
committeeditingstyle
方法只是从表(和数据源)中删除最后一行,那么您的
numberofrowsinssection
对于该节也必须返回0。但是如果有0行,则显示您的
numberOfSections
numberofrowsinsSection
忽略了整个部分。这就是导致不一致性错误的原因。