2013-05-21 68 views
0

我新來的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]; 

} 
+0

你可能確實是故意的,我不是在一臺Mac的前面去嘗試,但不應'#進口CoreData/CoreData.h'是'#import '?糾正我,如果我錯了。 – esh

+0

你必須刪除你的managedObject,但是在你的源代碼中你並沒有這樣做。 [_context deleteObject:webBrowser]; –

回答

0
[webBrowser deleteObjectat:indexPath.row]; 

大概應該是

[context deleteObject:webBrowser]; 

其中context是你的管理對象上下文。

+0

不是我的託管對象上下文「webDocument」我宣佈在頂部。這似乎並沒有工作。 –

+0

這是我的,它不工作。 WebBrowser * webBrowser = [self.resultsController objectAtIndexPath:indexPath]; [webDocument deleteObject:webBrowser]; –

+0

@MesbahJamali:您的託管對象上下文似乎是'self.webDocument.managedObjectContext',所以它應該是'WebBrowser * webBrowser = [self.resultsController objectAtIndexPath:indexPath]; [self.webDocument.managedObjectContext deleteObject:webBrowser]'。 –

0

我的方式做到了裏面的commitEditingStyle

if (editingStyle == UITableViewCellEditingStyleDelete) { 


     Entity *loopEntity = [self.tableArray objectAtIndex:indexPath.row]; 


     // Delete the managed object at the given index path. 
    NSManagedObject *eventToDelete = [self.tableArray objectAtIndex:indexPath.row]; 
    [self.context deleteObject:eventToDelete]; 

     // Update the array and table view. 
    [self.tableArray removeObjectAtIndex:indexPath.row]; 
    [self.ammoTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES]; 

     // Commit the change. 
    NSError *error = nil; 
    if (![self.context save:&error]) { 
      // Handle the error. 
    } 
}