Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/102.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 正在从UITableView中的SensorDeredSet中删除NSManagedObject_Ios_Objective C_Uitableview_Core Data - Fatal编程技术网

Ios 正在从UITableView中的SensorDeredSet中删除NSManagedObject

Ios 正在从UITableView中的SensorDeredSet中删除NSManagedObject,ios,objective-c,uitableview,core-data,Ios,Objective C,Uitableview,Core Data,我有一个UITableViewController,它可以正确显示与NSManagedObject关联的SensorDeredSet。在这个UITableView上,我配置了一个按钮,可以进行编辑,并且可以毫无问题地重新排序传感器数据集中的项目。当我试图删除时,什么也没发生——也许最糟糕的是,它没有崩溃 不幸的是,我无法将其配置为删除单元格。我错过了什么 MyviewDidLoad最初加载self.isEditing=NO - (IBAction)toggleEditButton:(UIBarB

我有一个
UITableViewController
,它可以正确显示与
NSManagedObject
关联的
SensorDeredSet
。在这个
UITableView
上,我配置了一个按钮,可以进行编辑,并且可以毫无问题地重新排序
传感器数据集中的项目。当我试图删除时,什么也没发生——也许最糟糕的是,它没有崩溃

不幸的是,我无法将其配置为删除单元格。我错过了什么

My
viewDidLoad
最初加载
self.isEditing=NO

- (IBAction)toggleEditButton:(UIBarButtonItem *)sender {
    // You need to do 2 things:
    // 1 - Edit
    if (self.isEditing == NO) {
        NSLog(@"isEditing is in edit mode");
        self.editButton.title = @"Done";
        self.isEditing = YES;
        [self.tableView setEditing:YES animated:YES];
    } else {
        // 2 - Save
        NSLog(@"isEditing is in save mode");
        self.editButton.title = @"Edit";
        self.isEditing = NO;
        [self.tableView setEditing:NO animated:YES];
    }
}
我配置了一个
ui按钮
,可在
YES
NO
之间切换
self.I编辑

- (IBAction)toggleEditButton:(UIBarButtonItem *)sender {
    // You need to do 2 things:
    // 1 - Edit
    if (self.isEditing == NO) {
        NSLog(@"isEditing is in edit mode");
        self.editButton.title = @"Done";
        self.isEditing = YES;
        [self.tableView setEditing:YES animated:YES];
    } else {
        // 2 - Save
        NSLog(@"isEditing is in save mode");
        self.editButton.title = @"Edit";
        self.isEditing = NO;
        [self.tableView setEditing:NO animated:YES];
    }
}
self.isEditing
设置为
YES
时,我可以在我的
SensoredSet
中移动项目,并保存重新排序的
SensoredSet

这是我的
承诺风格

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

        //
        [self.myManagedObject.myNSOrderedSet.mutableCopy removeObjectAtIndex:indexPath.row];
        self.myManagedObject.myNSOrderedSet = self.myManagedObject.myNSOrderedSet.mutableCopy;
        [tableView reloadData];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }
谢谢你的阅读。我欢迎你的意见

更新 我尝试了以下几种解决方案,但在这个
UITableViewController
上的行为仍然是相同的——会显示重新排序控件,但在编辑模式下没有删除和滑动到删除操作

我配置了一个
customCell
。布局约束会掩盖什么吗

更新2 针对Paulw11的问题,我实施了
CaneditRowatineXpath
EditingStyleforRowatineXpath
,是的。这是:

editingStyleForRowAtIndexPath

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
// Originally, code was:
// return UITableViewCellEditingStyleNone
    if (self.isEditing == YES) {
        return UITableViewCellEditingStyleDelete;
    } else {
        return UITableViewCellEditingStyleNone;
    }
}
这似乎是我的主要问题。我最初将其设置为
UITableViewCellEditingStyleNone
。我从来没有在重新安排工作时“碰到”问题,所以我没有想到去看那里。我更新了它如下

canEditRowAtIndexPath

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
// Originally, code was:
// return UITableViewCellEditingStyleNone
    if (self.isEditing == YES) {
        return UITableViewCellEditingStyleDelete;
    } else {
        return UITableViewCellEditingStyleNone;
    }
}
现在我得到了这个错误:

'nsinternalinconsistenceexception',原因:'无效更新:无效的
第0节中的行。之后现有节中包含的行数
更新(3)必须等于该节中包含的行数
在更新(3)之前,加上或减去从中插入或删除的行数
该部分(插入0,删除1)并加上或减去移动的行数
进入或离开该部分(0移入,0移出)。“


我想我可以从这里找到答案。我会在早上更新到最终解决方案——发现这个愚蠢的错误后,我会在晚上挂断马刺,并在早上重新开始:)

我认为问题在于这些方面:

[self.myManagedObject.myNSOrderedSet.mutableCopy removeObjectAtIndex:indexPath.row];
self.myManagedObject.myNSOrderedSet = self.myManagedObject.myNSOrderedSet.mutableCopy;
第一行创建有序集的可变副本,然后删除一个对象。 第二行创建原始集合的另一个可变副本

尝试分配给临时变量:

NSMutableOrderedSet *tempSet = self.myManagedObject.myNSOrderedSet.mutableCopy;
[tempSet removeObjectAtIndex:indexPath.row];
self.myManagedObject.myNSOrderedSet = tempSet;

我认为问题在于这些方面:

[self.myManagedObject.myNSOrderedSet.mutableCopy removeObjectAtIndex:indexPath.row];
self.myManagedObject.myNSOrderedSet = self.myManagedObject.myNSOrderedSet.mutableCopy;
第一行创建有序集的可变副本,然后删除一个对象。 第二行创建原始集合的另一个可变副本

尝试分配给临时变量:

NSMutableOrderedSet *tempSet = self.myManagedObject.myNSOrderedSet.mutableCopy;
[tempSet removeObjectAtIndex:indexPath.row];
self.myManagedObject.myNSOrderedSet = tempSet;

MySensorDeredSet
是核心数据关系属性吗?如果是这样,您应该使用

[[mymanagedObject mutableOrderedSetValueForKey:@"myNSOrderedSet"] removeObjectAtIndex:indexPath.row]
就这样;您不需要将其分配回
mySensorDeredSet


(请注意,根据表视图的数据源,您可能需要在对象的
NSManagedObjectContext
上调用
save

MySensorDeredSet
是核心数据关系属性吗?如果是这样,您应该使用

[[mymanagedObject mutableOrderedSetValueForKey:@"myNSOrderedSet"] removeObjectAtIndex:indexPath.row]
就这样;您不需要将其分配回
mySensorDeredSet


(注意,根据表视图的数据源,您可能需要在对象的
NSManagedObjectContext
上调用
save

事实证明,我遇到的大部分问题都是
编辑样式forrowatindexpath
。我最初的方法是这样的,当我只是对
表视图进行重新排序时,我没有碰过它:

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleNone;
}
paul11询问我的实现中是什么样子时,我更新到:

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.isEditing == YES) {
        return UITableViewCellEditingStyleDelete;
    } else {
        return UITableViewCellEditingStyleNone;
    }
}
此外,我更新了pbasdf的建议,如下所示:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete && self.routineToEdit.stretches.count > 2) {
        // this needs to be added to keep an consistency error from being thrown
        // you'll also need to end updates when it's done deleting
        [self.tableView beginUpdates];
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        // pbasdf suggested this for handling the NSOrderedSet
        NSMutableOrderedSet *tempSet = self.myNSManagedObject.myNSOrderedSet.mutableCopy;
        [tempSet removeObjectAtIndex:indexPath.row];
        self.myNSManagedObject.myNSOrderedSet = tempSet;
        [self.tableView endUpdates];
        // and you need to save the context so the deletion persists
        [self saveContext];

    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }
}

谢谢大家的意见

事实证明,我的大部分问题都生活在
editingStyleforRowatinexPath
中。我最初的方法是这样的,当我只是对
表视图进行重新排序时,我没有碰过它:

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleNone;
}
paul11询问我的实现中是什么样子时,我更新到:

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.isEditing == YES) {
        return UITableViewCellEditingStyleDelete;
    } else {
        return UITableViewCellEditingStyleNone;
    }
}
此外,我更新了pbasdf的建议,如下所示:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete && self.routineToEdit.stretches.count > 2) {
        // this needs to be added to keep an consistency error from being thrown
        // you'll also need to end updates when it's done deleting
        [self.tableView beginUpdates];
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        // pbasdf suggested this for handling the NSOrderedSet
        NSMutableOrderedSet *tempSet = self.myNSManagedObject.myNSOrderedSet.mutableCopy;
        [tempSet removeObjectAtIndex:indexPath.row];
        self.myNSManagedObject.myNSOrderedSet = tempSet;
        [self.tableView endUpdates];
        // and you need to save the context so the deletion persists
        [self saveContext];

    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }
}

谢谢大家的意见

如果调用
deleteRowsAtIndexPaths
,则不需要调用
reloadData
。您是否设置了断点以确认此方法是否实际执行?谢谢。根据您的建议,我在编辑模式下抛出了一个断点,但什么也没有发生(除了出现重新排序控件)。您是否实现了
CaneditRowatineIndexPath
EditingStyleForRowatineIndexPath
?如果是,请展示这些方法!我认为,
editingstyleforrowtaindexpath
是我的主要问题。我更新了帖子。我想我可以解决剩下的问题,因为我以前在第节中遇到过无效行。我会在早上把它转来转去,然后把我能得到的东西发出去。感谢您在这方面的帮助。我添加了
[self.tableview beginUpdates]
[self.tableview endUpdates]
,这就解决了不一致的问题。如果您调用
deleteRowsAtIndexPaths
,则无需调用
重新加载数据。您是否设置了断点以确认此方法是否实际执行?谢谢。在你的建议下,我在编辑模式下抛出了一个断点,但什么都没有发生(除了出现重新排序控件)。你实现了
canEditRow吗