Iphone UITableView deleteRowsAtIndexPath在删除最后一条记录时崩溃

Iphone UITableView deleteRowsAtIndexPath在删除最后一条记录时崩溃,iphone,ios,uitableview,Iphone,Ios,Uitableview,当我从UITableView中删除最后一条记录时,遇到以下错误 由于未捕获异常而终止应用程序 'NSInternalInconsistencyException',原因:'无效更新:无效 节0中的行数。表中包含的行数 更新(3)后的现有节数必须等于 更新(1)之前该节中包含的行,加号或减号 从该节插入或删除的行数(插入1, 1)并加上或减去移入或移出的行数 该部分(0移入,0移出)。' 我的目标是在表数组为空时显示“未找到记录” 这是我正在使用的代码。当我从表数组中删除最后一条记录时,应用程序崩

当我从
UITableView
中删除最后一条记录时,遇到以下错误

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

我的目标是在表数组为空时显示“未找到记录”

这是我正在使用的代码。当我从表数组中删除最后一条记录时,应用程序崩溃。如何重新加载表并显示“未找到记录”标签


问题在于tableview期望在视图上执行的操作与数据源匹配。表中有一条记录,然后将其删除。tableview希望数据源现在包含零条记录,但由于“找不到记录”逻辑,它实际上返回值3,因此出现一致性错误和崩溃

问题似乎是这一部分:

if ([idArray count] == 0) {
    [self.idTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
我假设这是为了在删除最后一行时将“未找到记录”行插入表中,但由于“未找到记录”实际上跨越三行,因此需要在此处插入三行,如下所示:

if ([idArray count] == 0) {
    [self.idTableView insertRowsAtIndexPaths:[NSArray arrayWithObjects:
    [NSIndexPath indexPathForRow:0 inSection:indexPath.section],
    [NSIndexPath indexPathForRow:1 inSection:indexPath.section],
    [NSIndexPath indexPathForRow:2 inSection:indexPath.section],
    nil] withRowAnimation:UITableViewRowAnimationFade];
}

然而,为了你自己的理智,我能建议一种不同的方法吗?与其试图保持表和数据源的同步,同时处理这些仅用于显示目的的伪三行数据,为什么不在视图层次结构中插入一个UILabel(在tableview前面或后面),上面写着“未找到记录”,并根据表中是否有任何数据显示/隐藏它?这样,您就可以精确地控制其位置和外观,而无需修改数据源逻辑

处理删除行的一般规则如下:

  • 处理你的模型
  • 处理行的动画
  • 例如:

    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
       if (editingStyle == UITableViewCellEditingStyleDelete) {
           NSInteger row = [indexPath row];
    
           [yourModel removeObjectAtIndex:row]; // you need to update your model
    
           [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]    withRowAnimation:UITableViewRowAnimationFade];
        }
    }
    
    现在,在我看来,正确的代码可能是以下代码(我写了一些注释来指导您)


    希望有帮助。

    我使用的方法与使用单元格发出“无行”警告的方法相同

    对我来说,这很有效:

    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
    
        [favs removeObjectAtIndex:indexPath.section];
    
        if ([favs count] == 0) {
            [tableView reloadRowsAtIndexPaths:[[NSArray alloc]initWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
            [tableView setEditing:NO animated:YES];
    
            // Remove Edit bar button item
            self.navigationItem.rightBarButtonItem = nil;
        }
        else {
            // Animate the deletion from the table.
            [tableView deleteRowsAtIndexPaths:[[NSArray alloc]initWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
        }
    }
    

    }

    应用程序在哪一行崩溃?什么控制台日志?请输入崩溃日志。-编辑:啊,没有。找到了。谢谢。谢谢。尼克。我将尝试在一定范围内运行,删除最后一行…但显示一个有趣的错误。我最终接受了你的建议,显示和UILabel,因为没有找到任何记录。
    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
        if (editingStyle == UITableViewCellEditingStyleDelete) {
            //Get the object to delete from the array.
            identifyObject = [appDelegate.idArray objectAtIndex:indexPath.row];
            [appDelegate removeID:identifyObject]; // update model first
    
            // now you can check model count and do what you want
            if ([appDelegate.idArray count] == 0) // I think you mean appDelegate.idArray
            {   
                // do what you want
                // with  [self.idTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            }
    
            else
            {
                [self.idTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            }
        }
    }
    
    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
    
        [favs removeObjectAtIndex:indexPath.section];
    
        if ([favs count] == 0) {
            [tableView reloadRowsAtIndexPaths:[[NSArray alloc]initWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
            [tableView setEditing:NO animated:YES];
    
            // Remove Edit bar button item
            self.navigationItem.rightBarButtonItem = nil;
        }
        else {
            // Animate the deletion from the table.
            [tableView deleteRowsAtIndexPaths:[[NSArray alloc]initWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
        }
    }