我有一个表格视图,它显示来自实体的记录列表。
如果我删除任何记录,它应该从两者中tableview
删除entity
这是 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
虽然实现文件是
IMSCategoryViewController.m
#import "IMSCategoryViewController.h"
#import "IMSAppDelegate.h"
#import "Category.h"
@interface IMSCategoryViewController ()
{
NSManagedObjectContext *context;
}
@end
@implementation IMSCategoryViewController
@synthesize managedObjectContext = _managedObjectContext;
@synthesize managedObjectModel = _managedObjectModel;
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
@synthesize categoryArray;
@synthesize arr;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// [self.tableView reloadData];
IMSAppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
context = [appDelegate managedObjectContext];
NSEntityDescription *category = [NSEntityDescription entityForName:@"Category" inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:category];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"is_active == 1"];
[request setPredicate:predicate];
[request setFetchBatchSize:25];
[request setEntity:category];
NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
NSArray *srotDesc = [[NSArray alloc]initWithObjects:sort, nil];
[request setSortDescriptors:srotDesc];
NSError *error;
NSMutableArray *results = [[context executeFetchRequest:request error:&error] mutableCopy];
if (results == nil) {
//error handle here
}
[self setArr:results];
NSLog(@"there is category array");
[self.tableView reloadData];
[self.arr count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.arr count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
Category *category = [self.arr objectAtIndex:indexPath.row];
// Configure the cell...
cell.textLabel.text = [category name];
cell.detailTextLabel.text = [category descript];
return cell;
}
该代码运行良好。它显示来自名为 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:]: unrecognized selector sent to instance 0x74474a0
No visible @interface for 'NSArray' declares the selector 'removeObjectAtIndex:'
并在这行代码上构建错误
。
[self.arr removeObjectAtIndex:[indexPath row]];
您的解决方案将不胜感激。
提前致谢。