删除最后一条记录时UITableView deleteRowsAtIndexPath崩溃 [英] UITableView deleteRowsAtIndexPath crash when delete last record

查看:1079
本文介绍了删除最后一条记录时UITableView deleteRowsAtIndexPath崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm encountering the following error when I delete the last record from a UITableView.


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

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 (1), plus or minus the number of rows inserted or deleted from that section (1 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

如果表数组为空,我的目标是显示No Record found。

My goal is to show "No Record found" if the table array is empty.

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

This is the code I'm using. When I delete the last record from table array the app crashes. How is it possible to reload the table and show "No Record Found" label?

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if ([idArray count]==0) {
        return  3;
    }
    else 
    {
        return [idArray count];
    }   
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSLog(@"array count %d",[idArray count]);
    if ([idArray count] == 0) {
        static NSString *CellIdentifier = @"Cell";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
        cell.textLabel.textAlignment = UITextAlignmentCenter;

        tableView.userInteractionEnabled = NO;

        self.navigationItem.leftBarButtonItem.enabled = NO;
         NSUInteger row = [indexPath row];

        switch (row) {
            case 0:
                cell.textLabel.text = @"";
                break;
            case 1:
                cell.textLabel.text = @"";
                break;

            case 2:
                cell.textLabel.text = @"No Records Found";
                break;


            default:
                break;
        }        

        return cell;
    }
    else
    {   static NSString *CellIdentifier = @"Cell";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        }

        tableView.userInteractionEnabled = YES;

        self.navigationItem.leftBarButtonItem.enabled = YES;

        // Set up the cell
        identify *idItems = [idArray objectAtIndex:indexPath.row];

        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"dd MMM,yyyy"];
        NSString *dateStr = [formatter stringFromDate:idItems.Date];
        UIImageView *accDis = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Arrow.png"]];    
        cell.accessoryView = accDis;
        self.idTableView.separatorColor = [UIColor colorWithRed:150.0/255.0 green:150.0/255.0 blue:150.0/255.0 alpha:1];
        cell.textLabel.textColor = [UIColor blackColor];
        cell.textLabel.font = [UIFont boldSystemFontOfSize:18];
        cell.textLabel.adjustsFontSizeToFitWidth = YES;
        cell.detailTextLabel.textColor = [UIColor colorWithRed:100.0/255.0 green:100.0/255.0 blue:100.0/255.0 alpha:1];
        cell.detailTextLabel.font = [UIFont italicSystemFontOfSize:16];
        cell.detailTextLabel.adjustsFontSizeToFitWidth = YES;

        NSString *detailText = [NSString stringWithFormat:@"%@ - %@",dateStr,idItems.GeoCode];

        if (idItems.Image == NULL) {
            cell.imageView.image = [UIImage imageNamed:@"icon58x58.png"];
        }
        else
        {
        //pass image to fix size 50 X 50
        //UIImage *newImage = [self postProcessImage:idItems.Image];
            cell.imageView.image = idItems.thumb;//newImage; 
        cell.imageView.contentMode=UIViewContentModeScaleAspectFill;
        }

        cell.textLabel.text = idItems.TypeName; 
        cell.detailTextLabel.text = detailText;

        return cell;   
    }    
}

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

    if(editingStyle == UITableViewCellEditingStyleDelete) {

        if ([idArray count] >=1) 
        {
            [idTableView beginUpdates];

            //Get the object to delete from the array.
            identifyObject = [appDelegate.idArray objectAtIndex:indexPath.row];

            //Delete the object from the table.
            [self.idTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
             [appDelegate removeID:identifyObject];

            if ([idArray count] == 0) {
                [self.idTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            }
            [idTableView endUpdates];
        }
    }
}


推荐答案

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

The problem is that a tableview expects the operations performed on the view to match the data source. You have one record in the table, and you remove it. The tableview is expecting the datasource to now contain zero records, but because of your "no records found" logic, it actually returns a value of 3, hence the consistency error, and your crash.

该错误似乎是这一部分:

The bug seems to be this part:

if ([idArray count] == 0) {
    [self.idTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}

我认为这是为了将未找到记录行插入删除最后一行时的表,但由于你的没有找到记录实际上跨越三行,你需要在这里插入三行,如下所示:

I assume this was intended to insert the "no records found" row into the table when the last line is deleted, but since your "no records found" actually spans three rows, you need to insert three rows here instead, like this:

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前面或后面),说没有找到记录并根据表格是否有任何数据显示/隐藏它?这样,您可以精确控制其位置和外观,而无需使用数据源逻辑。

For you own sanity however, can I suggest a different approach? Rather than trying to keep your table and datasource in sync whilst juggling these fake three rows of data that are only there for display purposes, why not just insert a UILabel into your view hierarchy (either in front of or behind the tableview) that says "no records found" and show/hide it based on whether the table has any data? That way you can precisely control its position and appearance without having to screw around with your datasource logic.

这篇关于删除最后一条记录时UITableView deleteRowsAtIndexPath崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆