【问题标题】:Remove NSManagedObject within commitEditingStyle在 commitEditingStyle 中删除 NSManagedObject
【发布时间】:2014-11-22 18:21:47
【问题描述】:

我还是 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"];

}

提前致谢。

【问题讨论】:

    标签: ios uitableview core-data nsmanagedobject nsmanagedobjectcontext


    【解决方案1】:

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

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

    【讨论】:

    • 我更改了代码。现在它说“'NSMutableArray' 没有可见的@interface 声明选择器'objectAtIndex'”
    • 好的 - 你真的有一个 fetchedResultsController 吗?
    • 不,我没有。 .
    • 你能显示更多代码吗?例如你的 cellForRowAtIndexPath 和 viewDidLoad?
    • viewDidLoad 只有[super viewDidLoad];。添加了 viewDidAppearcellForRowAtIndexPath
    【解决方案2】:

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

    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];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-27
      • 2011-08-06
      • 1970-01-01
      • 1970-01-01
      • 2014-12-09
      • 2023-03-29
      • 1970-01-01
      相关资源
      最近更新 更多