1

我正在从 coredata 中检索数据并将其显示在 tableview 中。如果用户单击一行,它将导航到另一个视图(例如视图 2)。

现在,我想从 tableview 以及 coredata 中删除一行。我使用了一个代码,可以删除该行。但是,当我去查看 2 并回来时,记录又回到了那里。(它已从 coredata 中删除,但该行及其数据仍然存在,直到我重新加载视图)。

这是我使用的代码。

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) 
    {
        NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
        [context deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]];
        NSError *error;
        if (![context save:&error]) 
        {
            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"App" message:@"Sorry the Item Cannot be deleted" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
            [alertView show];
            [alertView release];
        }
    }
}
4

3 回答 3

1

你有没有试过reloadData一次删除,

if (editingStyle == UITableViewCellEditingStyleDelete) 
  {
   NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
   [context deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]];
   NSError *error;
   if (![context save:&error]) 
     {
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Xpense Tracker" message:@"Sorry the Item Cannot be deleted" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alertView show];
        alertView release];
     }
   [YOURTABLEVIEW reloadData];
  }

编辑:它现在工作正常。试试这个方法

Step 1:

@interface ViewController : UIViewController <UITableViewDataSource,UITableViewDelegate,NSFetchedResultsControllerDelegate>

Step 2:

update your 
-(NSFetchedResultsController *)fetchedResultsController{
self.fetchedResultsController.delegate = self; // with this
}

step 3:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
    return [sectionInfo numberOfObjects];
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        NSManagedObjectContext * context = [self.fetchedResultsController managedObjectContext];
        [context deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]];
        NSError * error;
        if (![context save:&error])
        {
            NSLog(@"Deletion Error");
        }
    }

}

Step 4:

include NSFetchedResultsControllerDelegate method 
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
    [YOURTABLEVIEW reloadData];
}
于 2013-04-01T09:00:49.220 回答
1

NSManagedObjectContext删除后需要保存。

 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
    {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
        [context deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]];
        NSError *error;
        [context save:&error] //add this line

        if (![context save:&error]) {
            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Xpense Tracker" message:@"Sorry the Item Cannot be deleted" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
            [alertView show];
            [alertView release];

        }
    }

    }

检查这个..

于 2013-04-01T09:09:01.660 回答
0

根据您的要求更改您的表格和详细信息关系

- (void)tableView:(UITableView *)aTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (editingStyle == UITableViewCellEditingStyleDelete) {

        NSManagedObject *selectedOrder = [selectedOrderArray objectAtIndex:indexPath.row];
        Order *orderInstance = (Order *) selectedOrder;
        numberOfRows = [[orderInstance.detailRelation allObjects]count];

        NSString *deletedItem;

        deletedItem = (orderInstance.optionName == nil) ? [[NSString alloc]initWithFormat:@"%@ deleted successfully",orderInstance.menuName] :  [[NSString alloc]initWithFormat:@"%@ %@ deleted successfully",orderInstance.optionName , orderInstance.menuName];

        [self.managedObjectContext deleteObject:selectedOrder];        
        if([self performSaveInMemoryObjectContext:self.managedObjectContext]){

           // [self.tableview reloadData];
            [common ShowAlert:deletedItem andMessage:@""];
            [selectedOrderArray removeObjectAtIndex:indexPath.row];

        }
        [self fetchRecords];
     //   NSLog(@"count... %d",[selectedOrderArray count]);
        [tableview reloadData];

    }
}
于 2013-04-01T09:00:29.157 回答