NSInternalInconsistencyException(行数无效) [英] NSInternalInconsistencyException (invalid number of rows)

查看:125
本文介绍了NSInternalInconsistencyException(行数无效)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我在我的 UITableView 中有数据,我开始删除,它工作正常。但是,当我到达表中的最后一个对象,并删除它,它崩溃。

Whenever I have data in my UITableView and I start deleting, it works fine. However, when I get to the last object in the table, and delete it, it crashes.

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

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 (1) 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 (0 inserted, 1 deleted).'

这里是我做编辑的东西:

Here's how I'm doing the editing stuff:

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        if ([myData count] >= 1) {
            [tableView beginUpdates];
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            [myData removeObjectAtIndex:[indexPath row]];

            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString *documentsDirectory = [paths objectAtIndex:0];
            NSString *somepath = [documentsDirectory stringByAppendingPathComponent:@"something.plist"];
            [myData writeToFile:somepath atomically:YES];
            [table reloadData];
            if ([myData count] == 0) {
                [tableView endUpdates];
                [tableView reloadData];
            }
            else {
            [tableView endUpdates];
            }
        }
    }   
}

也是这样:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    if ([myData count] != 0) {
        return [myData count];
    }
    else {
        return 1;
    }
}

我返回1的原因是因为我'在 cellForRowAtIndexPath 中创建一个说明无数据保存的单元格。这是我的意思:

The reason I'm returning 1 is because I'm making a cell that says "No data saved" in cellForRowAtIndexPath. Here's what I mean:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }    
    if ([cityData count] != 0) {
        //normal setup removed for clarity
    }
    else {
        cell.textLabel.text = @"No saved data!";
    cell.textLabel.font = [UIFont boldSystemFontOfSize:14]; 
    cell.textLabel.textAlignment = UITextAlignmentCenter;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.tag = 1;
    return cell;
    }
}

因此,我在编辑代码时出错得到这个错误?谢谢!

So, what am I doing wrong in my editing code to get this error? Thanks!

推荐答案

如果删除表格中的最后一行, UITableView 代码期望剩余0行。它调用您的 UITableViewDataSource 方法来确定剩下多少。因为你有一个无数据单元格,它返回1,而不是0.所以当你删除表中的最后一行,尝试调用 -insertRowsAtIndexPaths:withRowAnimation:您的无数据行。此外,您不应在此方法的任何位置调用 -reloadData -endUpdates 将负责重新载入受影响的行。尝试这样:

If you delete the last row in your table, the UITableView code expects there to be 0 rows remaining. It calls your UITableViewDataSource methods to determine how many are left. Since you have a "No data" cell, it returns 1, not 0. So when you delete the last row in your table, try calling -insertRowsAtIndexPaths:withRowAnimation: to insert your "No data" row. Also, you should not call -reloadData anywhere in this method. -endUpdates will take care of reloading the affected rows. Try this out:

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        if ([myData count] >= 1) {
            [tableView beginUpdates];
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            [myData removeObjectAtIndex:[indexPath row]];

            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString *documentsDirectory = [paths objectAtIndex:0];
            NSString *somepath = [documentsDirectory stringByAppendingPathComponent:@"something.plist"];
            [myData writeToFile:somepath atomically:YES];

            if ([myData count] == 0) {
                [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            }
            [tableView endUpdates];
        }
    }   
}

这篇关于NSInternalInconsistencyException(行数无效)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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