2014-01-08 41 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];