0

我有这个功能:

- (void)initFetchRequest
{
    NSManagedObjectContext *context = self.managedObjectContext;
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    request.entity = [NSEntityDescription entityForName:@"Shindy" inManagedObjectContext:self.managedObjectContext];
    // Configure the request's entity, and optionally its predicate.
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"details" ascending:YES];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
    [request setSortDescriptors:sortDescriptors];

    fetchedResultsController = [[NSFetchedResultsController alloc]
                                              initWithFetchRequest:request
                                              managedObjectContext:context
                                              sectionNameKeyPath:nil
                                              cacheName:nil];

    NSError *error = nil;
    [self.managedObjectContext executeFetchRequest:request error:&error];
    [fetchedResultsController performFetch:&error];

    if (![self.fetchedResultsController performFetch:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    }    
}

在我的viewDidLoad方法中使用以下内容

NSURL *url = [NSURL fileURLWithPath:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]];
document = [[UIManagedDocument alloc] initWithFileURL:url];
self.managedObjectContext = document.managedObjectContext;

[self initFetchRequest];

最后,这在我的cellForRowAtIndexPath

Shindy *shindy = [fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = shindy.details;

数据是从另一个控制器输入的,当我回到这个控制器时,UITableViewController它没有更新。

我在这里想念什么?

4

1 回答 1

0

如果您从另一个视图控制器返回(例如在导航控制器中弹出),viewDidLoad则不会调用您的。因此您的数据不会被更新。如果你想让这种情况发生,你应该实现NSFetchedResultsControllerDelegate和 set fetchedResultsController.delegate = self;。每当发生更改时,它将更新您的表。有一个不太优雅的解决方案可以调用您的initFetchRequest方法,viewWillAppear但我会推荐第一个解决方案。希望这可以帮助!

于 2013-01-06T23:21:47.500 回答