Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 保存编辑的UITableView行_Ios_Uitableview - Fatal编程技术网

Ios 保存编辑的UITableView行

Ios 保存编辑的UITableView行,ios,uitableview,Ios,Uitableview,因此,基本上我有一个文件夹对象的表视图,我希望能够删除/删除文件夹。到目前为止,如果我尝试删除,文件夹将被删除,但当我重新运行应用程序时,它们将全部返回(因此删除不会保存)。有什么建议吗 以下是我对UITableView的删除方法: - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPat

因此,基本上我有一个文件夹对象的表视图,我希望能够删除/删除文件夹。到目前为止,如果我尝试删除,文件夹将被删除,但当我重新运行应用程序时,它们将全部返回(因此删除不会保存)。有什么建议吗

以下是我对
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;
}

它删除的原因是因为这一行:

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