gpt4 book ai didi

ios - UITableView 单元格删除崩溃?

转载 作者:行者123 更新时间:2023-11-28 22:44:58 26 4
gpt4 key购买 nike

所以我有这段代码来删除我的 TableView 中的一行:

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

现在我做 indexPath.row/2 因为我这样做:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [cellArray count] * 2;
}

只是因为它让我的 UI 看起来像现在这样。不管怎样,这就是崩溃:

*** 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 (42) must be equal to the number of rows contained in that section before the update (44), 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 {
NSIndexPath *nextPath = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section];
[self.cellArray removeObjectAtIndex:indexPath.row/2];
[[NSUserDefaults standardUserDefaults] setObject:self.cellArray forKey:@"cellArray"];
[tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObjects:indexPath, nextPath, nil] withRowAnimation:UITableViewRowAnimationFade];
}

最佳答案

问题是您在元胞数组中为每个对象使用了两个表行。但是在您发布的代码中,您只从表中删除了一行。您需要删除两行。

NSIndexPath *nextPath = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section];
[tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObjects:indexPath, nextPath, nil] withRowAnimation: UITableViewRowAnimationFade];

顺便说一句 - YES 不是行动画的有效参数。

关于ios - UITableView 单元格删除崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13444879/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com