当按下“编辑”然后按下“删除”按钮时,我试图删除表格视图中选定的核心数据元素。我似乎无法做到这一点。
我最后的方法是删除应该去的地方。按下删除按钮时成功调用该方法,但我不断收到错误“在“WebBrowser”处没有可见的@interface声明选择器“deleteObject”
这是我的代码的一部分。
#import "WebTableViewController2.h"
#import "WebBrowser.h"
#import CoreData/CoreData.h
#import "EditViewController.h"
@interface WebTableViewController2 ()
@property UIManagedDocument *webDocument;
@property NSFetchedResultsController *resultsController;
@end
@implementation WebTableViewController2
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *fileName = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent: @"WebDB.db"];
NSURL *fileURL = [NSURL fileURLWithPath: fileName];
self.webDocument = [[UIManagedDocument alloc] initWithFileURL:fileURL];
if (![[NSFileManager defaultManager] fileExistsAtPath:fileName])
{
[self.webDocument saveToURL:fileURL forSaveOperation: UIDocumentSaveForCreating completionHandler:^(BOOL success)
{
if(!success) NSLog(@" Database creation failed");
[self fetchData];
}];
}else
{
[self.webDocument openWithCompletionHandler:^(BOOL success)
{
if(!success) NSLog(@" Database opening failed");
[self fetchData];
}];
}
}
-(void)viewWillAppear:(BOOL)animated
{
self.navigationItem.leftBarButtonItem = self.editButtonItem;
}
- (void) fetchData
{
NSFetchRequest *request= [NSFetchRequest fetchRequestWithEntityName: @"WebBrowser"];
request.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"name1" ascending:YES]];
self.resultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:
self.webDocument.managedObjectContext sectionNameKeyPath:Nil cacheName:Nil];
self.resultsController.delegate = self;
/*
NSError *error = nil;
NSUInteger count = [self.webDocument.managedObjectContext countForFetchRequest:request error:&error];
//[request release];
NSLog(@"this is the number in data base: %i", count);
*/
NSError *error = nil;
if([self.resultsController performFetch: &error]){
[self.tableView reloadData];
}else{
NSLog(@"Could not fetch data: %@, %@", error, error.userInfo);
}
}
-(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
//deleting goes here
NSLog(@"deleting");
WebBrowser *webBrowser = [self.resultsController objectAtIndexPath:indexPath];
[webBrowser deleteObjectat:indexPath.row];
}