0

当按下“编辑”然后按下“删除”按钮时,我试图删除表格视图中选定的核心数据元素。我似乎无法做到这一点。
我最后的方法是删除应该去的地方。按下删除按钮时成功调用该方法,但我不断收到错误“在“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];

}
4

2 回答 2

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.
    }
}
于 2013-05-23T15:22:39.457 回答
0
[webBrowser deleteObjectat:indexPath.row];

应该是

[context deleteObject:webBrowser];

context您的托管对象上下文在哪里。

于 2013-05-21T05:30:49.217 回答