ios - 从UITableView中的NSOrderedSet中删除NSManagedObject

标签 ios objective-c uitableview core-data

我有一个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/

相关文章:

ios - WebRTC DataChannel 未在 iOS native 应用程序上打开

ios - 在 swift 的动态表格 View 单元格中使用 JSON 响应

ios - 将 UIVisualEffectView 添加到 UITableView 的背景 View 仅适用于模拟器而非设备

ios - NSPredicate 和丹麦字母 å 给出了错误的结果

objective-c - iOS 6 上的 XMPP 类中的警告

ios - iPhone App Building Setup - 相同的代码不同的 Assets

ios - 当应用程序在后台运行时显示 UIAlert?

ios - swift fatal error : unexpectedly found nil while unwrapping an Optional value (lldb) in Tableview

ios - 如何设置 UIDatePicker View 的日期格式

ios - (Swift)如何通过触摸按钮更改 pageviewcontroller 中的 View