gpt4 book ai didi

ios - 从UITableView中的NSOrderedSet中删除NSManagedObject

转载 作者:行者123 更新时间:2023-12-01 17:50:52 25 4
gpt4 key购买 nike

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

不幸的是,我无法对其进行配置以删除单元格。我想念什么?

我的viewDidLoad最初加载self.isEditing = NO

我配置了一个UIButton,可以在self.isEditingYES之间切换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];
}
}

self.isEditing设置为 YES时,我可以在 NSOrderedSet中移动项目并保存重新排序的 NSOrderedSet

这是我的 commitEditingStyle
- (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的Q,我实现了 canEditRowAtIndexPatheditingStyleForRowAtIndexPath,是的。他们来了:

editingStyleForRowAtIndexPath
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}

这似乎是我的主要问题。我最初将其设置为 UITableViewCellEditingStyleNone。我从不对工作进行重新排序而遇到任何问题,因此我不认为应该去那里。我更新如下。

canEditRowAtIndexPath
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
// Originally, code was:
// return UITableViewCellEditingStyleNone
if (self.isEditing == YES) {
return UITableViewCellEditingStyleDelete;
} else {
return UITableViewCellEditingStyleNone;
}
}

现在我得到这个错误:
'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).'

我想我可以从这里弄清楚。我会在早上更新为最终解决方案-在发现这个愚蠢的错误并在早上提起时,打算在晚上挂断我的马刺:)

最佳答案

我认为问题出在以下几行:

[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;

关于ios - 从UITableView中的NSOrderedSet中删除NSManagedObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31734877/

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