在commitEditingStyle中删除NSManagedObject

时间:2014-11-22 18:21:47

标签: ios uitableview core-data nsmanagedobject nsmanagedobjectcontext

我仍然是IOS开发的新手。我需要从ViewController中删除一个对象,但是XCode说fetchedResultsController是未知的:

#import "ViewController.h"
#import "CoreData/CoreData.h"

@interface ViewController ()

@property (strong) NSMutableArray *devices;

@end

@implementation ViewController

- (NSManagedObjectContext *)managedObjectContext
{
    NSManagedObjectContext *context = nil;
    id delegate = [[UIApplication sharedApplication] delegate];
    if ([delegate performSelector:@selector(managedObjectContext)]) {
        context = [delegate managedObjectContext];
    }
    return context;
}

...

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        // two following lines probably are erroneous 
        NSManagedObject *entityToDelete =
        [fetchedResultsController objectAtIndexPath:indexPath];
        [mangedObjectContext deleteObject:entityToDelete];


        // Remove the row from data model
        [self.devices removeObjectAtIndex:indexPath.row];

        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }

}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    // Fetch the devices from persistent data store
    NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Device"];
    self.devices = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];

    [self.tableView reloadData];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"RecipeCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }

    NSManagedObject *device = [self.devices objectAtIndex:indexPath.row];

    cell.textLabel.text = [NSString stringWithFormat:@"%@ %@", [device valueForKey:@"name"], [device valueForKey:@"version"]];
    cell.detailTextLabel.text = [device valueForKey:@"company"];

}

提前致谢。

2 个答案:

答案 0 :(得分:0)

您似乎使用self.devices作为数据模型,而不是fetchedResultsController。如果是这样,您应该获得对要从该数组中删除的对象的引用:

    NSManagedObject *entityToDelete =
    [self.devices objectAtIndex:indexPath.row];
    [mangedObjectContext deleteObject:entityToDelete];

答案 1 :(得分:0)

好的,我解决了这个问题:

NSManagedObjectContext *context = [self managedObjectContext];
if (editingStyle == UITableViewCellEditingStyleDelete)
{
    // Delete object from database
    [context deleteObject:[self.devices objectAtIndex:indexPath.row]];


    // Remove the row from data model
    [self.devices removeObjectAtIndex:indexPath.row];

    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}