我想从我的 tableviewcontroller 单元格中删除记录。
这是我的 ViewController.h 文件的代码
#import <UIKit/UIKit.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
而这个在实现文件中。
#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;
以及我在哪里执行删除操作。
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSManagedObject *recordtoDelete = [arr objectAtIndex:indexPath.row];
[_managedObjectContext deletedObject:recordtoDelete];
[arr objectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
NSError *error = nil;
if (![_managedObjectContext save:&error]) {
//handle error here
}
}
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
}
}
好的所以当我运行这段代码时......
它给出了这种类型的错误......
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason:'
Invalid update: invalid number of rows in section 0.
The number of rows contained in an existing section after the update (15) must be equal
to the number of rows contained in that section before the update (15), 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).'