删除行时 Tableview 崩溃?

发布于 2024-12-02 17:20:18 字数 1800 浏览 0 评论 0原文

我从表格视图中删除单元格时发生崩溃。我正在使用从 NSUserDefaults 加载的 NSMutableArray 将单元格加载到表格视图中。 这是崩溃的情况: 代码:

2011-09-03 18:21:06.097 App[10356:707] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit/UIKit-1906/UITableView.m:1046
2011-09-03 18:21:06.100 App[10356:707] *** 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 (3), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

这就是我删除行的方法... 代码:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.cellArray objectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath] withRowAnimation:YES];
    [[NSUserDefaults standardUserDefaults] setObject:cellArray forKey:@"cellArray"];
}

这是我的 numberOfRowsInSection 委托方法: 代码: // 自定义表视图中的行数。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    self.cellArray = [NSMutableArray array];
    [self.cellArray addObjectsFromArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"cellArray"]];

    if ([self.cellArray count] == 0) {
        [ivstatstableView setHidden:YES];
        [sortbar setEnabled:NO];
    }
    return [cellArray count];
}

知道这里出了什么问题吗?

I am having a crash with deleting a cell from a tableview. I am using a NSMutableArray loaded from NSUserDefaults to load the cells into the tableview.
This is the crash:
Code:

2011-09-03 18:21:06.097 App[10356:707] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit/UIKit-1906/UITableView.m:1046
2011-09-03 18:21:06.100 App[10356:707] *** 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 (3), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

This is how I delete the rows...
Code:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.cellArray objectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath] withRowAnimation:YES];
    [[NSUserDefaults standardUserDefaults] setObject:cellArray forKey:@"cellArray"];
}

This is my numberOfRowsInSection delegate method:
Code:
// Customize the number of rows in the table view.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    self.cellArray = [NSMutableArray array];
    [self.cellArray addObjectsFromArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"cellArray"]];

    if ([self.cellArray count] == 0) {
        [ivstatstableView setHidden:YES];
        [sortbar setEnabled:NO];
    }
    return [cellArray count];
}

Any idea what is wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

当梦初醒 2024-12-09 17:20:18

从数组中删除对象后,您需要调用reloadData。当您调用 reloadData 方法时,您的新数组将被更新,这让您的 UITableView 根据新更新的数据加载新单元格。

You need to call reloadData after removing an object from your array. When you call reloadData method, your new array is updated which lets your UITableView load new cells based on the new updated data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文