2014-12-29 2 views
0

Так что в основном у меня есть табличное представление объектов папки, и я хочу иметь возможность удалять/удалять папки. Пока я пытаюсь удалить, папки удаляются, но при повторном запуске приложения все они возвращаются (поэтому удаление не сохраняется). Любой совет?Сохранение отредактированных строк UITableView

вот мой метод удаления для UITableView:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     // Delete the row from the data source 

     [self.folders removeObjectAtIndex:indexPath.row]; 
     NSMutableArray *newSavedFolders = [[NSMutableArray alloc] init]; 

     for (Folder *folder in self.folders){ 
      [newSavedFolders addObject:[self folderWithName:folder.name]]; 
     } 

     [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 

    } else if (editingStyle == UITableViewCellEditingStyleInsert) { 
     // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view 
    } 
} 

и метод folderWithName отсюда:

- (Folder *)folderWithName:(NSString *)name { 
    id delegate = [[UIApplication sharedApplication] delegate]; 
    NSManagedObjectContext *context = [delegate managedObjectContext]; 

    Folder *folder = [NSEntityDescription insertNewObjectForEntityForName:@"Folder" inManagedObjectContext:context]; 
    folder.name = name; 
    folder.date = [NSDate date]; 

    NSError *error; 
    if (![context save:&error]) { 
     //we have an error 
    } 

    return folder; 
} 

ответ

0

Причина удаляет из-за этой линии:

[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 

Это удаляет строку из представления, но не удаляет данные.

Если вы используете CoreData, то вы просто сделать следующее с NSFetchedResultsController:

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


if (editingStyle == UITableViewCellEditingStyleDelete) { 
    NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext]; 
    [context deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]]; 
    [self.tableView reloadData]; 
    NSError *error = nil; 
    if (![context save:&error]) { 
     NSLog(@"Can't Delete! %@ %@", error, [error localizedDescription]); 
     return; 
    } 


} else if (editingStyle == UITableViewCellEditingStyleInsert) { 
    // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view 
} 

}

Добавить вызов ваши детали с NSFetchedResultsController таким образом:

#pragma mark - Fetched results controller 

- (NSFetchedResultsController *)fetchedResultsController 
{ 
if (fetchedResultsController != nil) { 
    return fetchedResultsController; 
} 

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
// Edit the entity name as appropriate. 
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Folder" inManagedObjectContext:self.managedObjectContext]; 
[fetchRequest setEntity:entity]; 

// Set the batch size to a suitable number for displaying in UITableView. 
[fetchRequest setFetchBatchSize:20]; 

// Edit the sort key as appropriate. 
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES]; 
NSArray *sortDescriptors = @[sortDescriptor]; 

[fetchRequest setSortDescriptors:sortDescriptors]; 

NSError *error = nil; 
if (![self.fetchedResultsController performFetch:&error]) { 
    // Replace this implementation with code to handle the error appropriately. 
    // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
    abort(); 
} 

return fetchedResultsController; 
} 
Смежные вопросы