我有一個表格視圖,它顯示來自實體的記錄列表。如何從表視圖行和核心數據實體中刪除數據?
如果我刪除它應該從兩個tableview
和entity
刪除任何記錄這畝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;
}
該守則工作得很好。它顯示來自名爲類別的實體的記錄。
現在什麼都要在這裏完成,我想根據上面的代碼刪除記錄...
- (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]];
您的解決方案將非常感激。
提前感謝。
1)如何構建錯誤**和**運行時異常?無論您的代碼是否編譯。 2)如果你想從它中刪除對象,'self.arr'必須是一個NSMutableArray。 3)你應該看看NSFetchedResultsController。 –
馬丁先生如果我嘗試了一些你想知道的其他問題嗎? –
@MartinR先生可以,如果你說.... –