我新來的Xcode和iPhone應用程序,所以請裸露在我身邊。在特定位置刪除核心數據元素。
當「編輯」,然後按「刪除」按鈕時,我試圖刪除我的表視圖中選定的核心數據元素。雖然我似乎無法做到這一點。
我最後的方法是刪除應該去的地方。當按下刪除鍵這種方法被調用成功,但我不斷收到錯誤web瀏覽器「,在沒有明顯的@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];
}
你可能確實是故意的,我不是在一臺Mac的前面去嘗試,但不應'#進口CoreData/CoreData.h'是'#import'?糾正我,如果我錯了。 –
esh
你必須刪除你的managedObject,但是在你的源代碼中你並沒有這樣做。 [_context deleteObject:webBrowser]; –