在 commitEditingStyle 中删除 NSManagedObject

Posted

技术标签:

【中文标题】在 commitEditingStyle 中删除 NSManagedObject【英文标题】:Remove NSManagedObject within commitEditingStyle 【发布时间】: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"];


提前致谢。

【问题讨论】:

【参考方案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];

【讨论】:

以上是关于在 commitEditingStyle 中删除 NSManagedObject的主要内容,如果未能解决你的问题,请参考以下文章

UITableView 删除行,commitEditingStyle 方法没有触发?

UITableView commitEditingStyle 不删除单元格

iOS - 如何从 commitEditingStyle 获取表格行的标签

commitEditingStyle 中的 NSIndexPath

在 iPhone 的 Objective-C 中在 (commitEditingStyle) 之外调用 (deleteRowsAtIndexPaths)

崩溃:commitEditingStyle -deleteObject 为零