gpt4 book ai didi

ios - 从 UITableView 中删除行时以 NSException 类型的未捕获异常终止

转载 作者:行者123 更新时间:2023-11-29 12:42:29 25 4
gpt4 key购买 nike

我正在开发一个应用程序,当用户移动超过预设距离间隔时,该应用程序将更新用户的坐标并将其添加到 UITableView。坐标将自动添加到一个 NSMutableArray 中,我使用该数组来更新表格。

一切都加载并工作正常(我可以通过移动和重新排序行来编辑表格)但是每当我选择删除特定行时,程序崩溃并出现错误“libc++abi.dylib:终止于NSException 类型的未捕获异常。

- (void) tableView:( UITableView *) tableView commitEditingStyle:( UITableViewCellEditingStyle) editingStyle forRowAtIndexPath:( NSIndexPath *) indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {

NSArray *items = [[G5SharedStore sharedStore]allCoords];
G5SharedStore *item = items[indexPath.row];
[[G5SharedStore sharedStore]removeItem:item];
//THIS IS WHERE THE ERROR OCCURS
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}

最后一行是导致错误的原因,但我是 objective-c 的新手,所以我不确定如何解决这个问题。任何帮助,将不胜感激。如果您需要更多详细信息,请告诉我。

提前致谢。

**编辑:好吧,我今天早上玩过代码并且它有效。问题是数组不断添加越来越多的项目,但 tableview 没有,除非我返回并再次单击“显示表”按钮以刷新它。因此,每当我删除 TableView 中的某些内容时,表的大小与数组的大小不一致,因此我得到错误。这是我的新问题,我试图通过使用以下方法使表自动更新其数据来解决上述问题:

[tableView reloadData];

表格确实更新了,但它不断添加空白单元格……其中没有任何数据。这是我添加以上内容的地方:

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@" UITableViewCell"
forIndexPath:indexPath];

NSArray *items = [[G5SharedStore sharedStore] allCoords];
G5SharedStore *item = items[indexPath.row];
cell.textLabel.text = [item description];
[tableView reloadData];
return cell;
}

因为我 1 个月前才开始 iOS 编程,所以我可能是错的。所以请指导我。

再次感谢大家

最佳答案

我可以看到两个问题:

首先this article建议在数据模型之前修改 TableView (您正在以相反的方式进行):

It must do two things:

Send deleteRowsAtIndexPaths:withRowAnimation: or insertRowsAtIndexPaths:withRowAnimation: to the table view to direct it to adjust its presentation.

Update the corresponding data-model array by either deleting the referenced item from the array or adding an item to the array.

其次,您似乎没有围绕该调用调用 beginUpdatesendUpdates。引用reference :

Note the behavior of this method when it is called in an animation block defined by the beginUpdates and endUpdates methods.

关于ios - 从 UITableView 中删除行时以 NSException 类型的未捕获异常终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24526066/

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