Ios 删除具有相应Parse.com数据的tableView行
我试图同时删除tableView行及其相应的parse.com数据。但是我似乎无法获取数据来重新加载和删除tableView行。但是,当我在单元格上单击delete时,我可以从parse中删除数据,但应用程序将崩溃。重新启动应用程序后,tableview行将具有正确的当前数据。下面是我试图删除该行的方式Ios 删除具有相应Parse.com数据的tableView行,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我试图同时删除tableView行及其相应的parse.com数据。但是我似乎无法获取数据来重新加载和删除tableView行。但是,当我在单元格上单击delete时,我可以从parse中删除数据,但应用程序将崩溃。重新启动应用程序后,tableview行将具有正确的当前数据。下面是我试图删除该行的方式 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editin
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView beginUpdates];
if (editingStyle == UITableViewCellEditingStyleDelete)
{
PFUser *user = [PFUser currentUser];
[user removeObject:[[user objectForKey:@"favoritedTracksArray"]objectAtIndex:indexPath.row] forKey:@"favoritedTracksArray"];
[user saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error)
{
[user refreshInBackgroundWithBlock:^(PFObject *object, NSError *error) {
if (!error) {
[[[[self.tracks objectAtIndex:0]objectForKey:@"favoritedTracksArray"]mutableCopy]removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationLeft];
[tableView reloadData];
[tableView endUpdates];
}
else NSLog(@"%@", error);
}];
}
else NSLog(@"%@",error);
}];
}
}
如您所见,我正在从Parse.com中删除PFObject,并保存更改。如果保存成功,我将刷新解析对象上的数据。如果保存成功,则删除数组的对象并删除相应的行,然后重新加载tableView的数据。
显然这是不正确的,因为我得到了这个错误
由于未捕获异常“NSInternalInconsistencyException”而终止应用程序,原因:“无效更新:节0中的行数无效。更新(1)后现有节中包含的行数必须等于更新(1)前该节中包含的行数。”,加上或减去从该节插入或删除的行数(0插入,1删除)和加上或减去移入或移出该节的行数(0移入,0移出)。”
发生碰撞的原因如下:
[[[[self.tracks objectAtIndex:0]objectForKey:@"favoritedTracksArray"]mutableCopy]removeObjectAtIndex:indexPath.row];
我真的很讨厌它的外观,所以我将格式化它:
NSDictionary *tracksDictionary = [self.tracks objectAtIndex:0];
NSArray *favoriteTracks = [tracksDictionary objectForKey:@"favoritedTracksArray"];
NSMutableArray *mutableFavoriteTracks = [favoriteTracks mutableCopy];
[mutableFavoriteTracks removeObjectAtIndex:indexPath.row];
您正在创建数组的副本并删除其中的对象。这不会影响提供数据源的原始阵列/容器
因此,您删除的是表视图中的一行,而不是数据源中的一行
与其这样做,不如将
mutableFavoriteTracks
设为属性,这将表示表视图的数据源,并在需要时从此数组中删除轨迹。天哪,您不需要编写[[[[[self.tracks objectAtIndex:0]objectForKey:@“favoritedTracksArray”]mutableCopy]removeObjectAtIndex:indexath.row]
在一行中。一行最多应该包含两个。是的,我不得不离开我的电脑几个小时,我意识到那行代码是多么愚蠢。对不起,lmao,我是一名新开发人员,仍在学习诀窍。不过我还是成功了,非常感谢你。