Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.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 如何从表视图行和核心数据实体中删除数据?_Ios_Objective C_Core Data_Uitableview - Fatal编程技术网

Ios 如何从表视图行和核心数据实体中删除数据?

Ios 如何从表视图行和核心数据实体中删除数据?,ios,objective-c,core-data,uitableview,Ios,Objective C,Core Data,Uitableview,我有一个表视图,它显示实体中的记录列表 如果我删除了任何记录,它应该同时从表视图和实体 这是mu IMSCategoryViewController.h文件 #import <UIKit/UIKit.h> #import <CoreData/CoreData.h> @interface IMSCategoryViewController : UITableViewController @property (readonly, strong, nonatomic) NSM

我有一个表视图,它显示实体中的记录列表

如果我删除了任何记录,它应该同时从
表视图
实体

这是mu IMSCategoryViewController.h文件

#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>

@interface IMSCategoryViewController : UITableViewController
@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
@property(nonatomic,retain)NSArray *arr;
@property (readonly, strong, nonatomic) NSMutableArray *categoryArray;
@end
代码运行得很好。它将显示名为Category的实体中的记录

现在应该怎么做这里我想删除记录根据上述代码

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    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
    }   
}
如果我尝试这个代码

    NSManagedObject *selectedObject = [self.arr objectAtIndex:[indexPath row]];
    [_managedObjectContext deleteObject:selectedObject];

    [self.arr removeObjectAtIndex:[indexPath row]];

    [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
这给了我一个例外

-[NSFetchRequest delete:]:无法识别的选择器发送到实例0x74474a0

和一个构建错误
对于'NSArray'没有可见的@interface声明选择器'removeObjectAtIndex:'
在这行代码上

[self.arr removeObjectAtIndex:[indexPath行]]

我们将非常感谢您的解决方案


提前向您表示感谢。

我认为,为了从数组中删除对象,您的arr必须反汇编NSMutableArray而不是NSArray

@property(nonatomic,retain)NSArray *arr;

arr是不可变数组,您不能从中添加或删除对象。

yes,executeFetchRequest除外:错误:方法返回NSArray对象,因此您应该使用NSMutableArray*results=[[NSMutableArray arrayWithArray:[[context executeFetchRequest:请求错误:&错误]mutableCopy]];但如果再次运行应用程序,则不会将其从核心数据实体中删除,然后数据会再次显示。请使用[self.context save:&error]方法来保存您对当前contextbro所做的任何更改这不是我的意思让我们来看看1)您怎么会有生成错误和运行时异常?您的代码是否编译。2)
self.arr
必须是NSMutableArray才能从中删除对象。3)您应该看看NSFetchedResultsController。Martin先生如果“我想知道还有其他一些问题你想知道吗?”MartinR先生,如果你说……如果你对NSFetchedResultsController有问题(你自己解决不了!),那么你可以发布一个新问题,可能有人会回答。
@property(nonatomic,retain)NSArray *arr;