删除特定位置的核心数据元素

时间:2013-05-21 04:45:36

标签: iphone objective-c core-data

当我按下“编辑”然后按下“删除”按钮时,我正在尝试删除表格视图中选定的核心数据元素。我似乎无法这样做。
我的最后一种方法是删除应该去的地方。按下删除按钮时成功调用该方法,但我不断收到错误“没有可见@interface at”WebBrowser“声明选择器”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];

}

2 个答案:

答案 0 :(得分:0)

[webBrowser deleteObjectat:indexPath.row];

应该是

[context deleteObject:webBrowser];

其中context是您的托管对象上下文。

答案 1 :(得分: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.
    }
}