0

저는 아직 IOS 개발에 초보자입니다. 내 ViewController에서 개체를 제거 할 필요가 있지만, 엑스 코드는 fetchedResultsController를 알 수없는 말 : 사전에commitEditingStyle 내의 NSManagedObject를 제거하십시오.

#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"]; 

} 

감사합니다.

답변

0

fetchedResultsController가 아니라 데이터 모델로 self.devices을 사용하는 것 같습니다. 그렇다면, 당신은 그 배열에서 삭제하려면 개체에 대한 참조를 얻어야한다 :

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

나는 코드를 변경했습니다. 이제는 "NSMutableArray가 'objectAtIndex'선택기를 선언한다는 표시가 없습니다. –

+0

Ok - 실제로 fetchedResultsController가 있습니까? – pbasdf

+0

아니요,하지 않았습니다. . –

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