2014-01-08 42 views
0

我试图删除核心数据中的一个对象,当你按下滑动并按删除。问题是它删除了表格单元格,但是当我返回时,再次删除的单元格又回来了。我想这是因为我只删除了NSMUtableArray(设备)中的对象,并没有删除核心数据对象。我怎样才能做到这一点?删除核心数据对象和表格

其中对象将被保存的saveTap:

-(void)saveTap:(id)sender{ 
    Entity *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:@"Entity" inManagedObjectContext:_managedObjectContext]; 
    [newManagedObject setValue:self.textfield.text forKey:@"playlistName"]; 
    [newManagedObject setValue:[NSNumber numberWithInt:selectedRowValue] forKey:@"idnumber"]; 



    // Save the context. 
    NSError *error = nil; 
    if (![_managedObjectContext save:&error]) { 

     NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
     abort(); 

    } 

    [self fetchDevices]; 

    NSLog(@"COUNT %d", [devices count]); 
} 

和commiteditingstyle方法

- (void)tableView:(UITableView *)tableView commitEditingStyle:  (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     [self.devices removeObjectAtIndex:indexPath.row]; 
     [tableViewData reloadData]; } 
} 

回答

1

是[self.devices objectAtIndex:indexPath.row];返回你想要删除的NSManagedObject? 那么你应该作出这样的第二个功能:

- (void)tableView:(UITableView *)tableView commitEditingStyle:  (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     [_managedObjectContext deleteObject:[self.devices objectAtIndex:indexPath.row]]; 
     [_managedObjectContext save:nil]; 
     [self.devices removeObjectAtIndex:indexPath.row]; 
     [tableViewData reloadData]; } 
} 
0

相反重装的同时UITableView的,你应该只reloadrowatindexpaths:和之前[UITableView的beganEditing] 和重新加载行 [UITableView endEditing];