我有这个功能:
- (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
没有更新。
我在这里缺少什么?
答案 0 :(得分:0)
如果您从另一个视图控制器返回(例如,在导航控制器中弹出),则不会调用viewDidLoad
。因此,您的数据不会更新。如果您希望这种情况发生,您应该实施NSFetchedResultsControllerDelegate并设置fetchedResultsController.delegate = self;
。每当发生更改时,它都会更新您的表。有一个不太优雅的解决方案,从initFetchRequest
调用您的viewWillAppear
方法,但我会推荐第一个解决方案。希望这有帮助!