tableView reloadData不起作用

时间:2011-12-08 20:41:42

标签: iphone uitableview core-data reloaddata viewwillappear

我在标签栏应用程序中有一个tableview。

我正在加载viewDidLoad

中的数据
managedObjectContext = nil;
managedObjectContext = [(RecipesAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"BrilliantMustache" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"badge.length > 0"];
[fetchRequest setPredicate:predicate];
NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"badge" ascending:YES];
NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor1,sortDescriptor2, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:@"badge" cacheName:nil];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
[aFetchedResultsController release];
[fetchRequest release];
[sortDescriptor1 release];
[sortDescriptor2 release];
[sortDescriptors release];

我正在尝试重新加载viewWillAppear

中的数据
- (void)viewWillAppear:(BOOL)animated {
    [self.tableView reloadData];
}

在此视图中,我调用另一个视图,该视图在更改时会在发布时更新核心数据。当我导航回tableview时,它不会更新。它将更新的唯一方法是,如果我退出应用程序并从头开始重新加载。

总共有2个单独的表视图。奇怪的是,第一个tableview正在刷新。

有什么想法? 罗布

2 个答案:

答案 0 :(得分:2)

您需要在viewDidLoad viewWillAppear中执行的操作。 viewDidLoad仅在启动时调用,因此尽管更改了数据,但您的视图从不更改其自己的本地数据,因此当您调用reloadData时,它仍会使用旧数据。

编辑:

你应该拥有什么:

- (void)viewWillAppear:(BOOL)animated {
    managedObjectContext = nil;
    managedObjectContext = [(RecipesAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"BrilliantMustache" inManagedObjectContext:managedObjectContext];
    [fetchRequest setEntity:entity];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"badge.length > 0"];
    [fetchRequest setPredicate:predicate];
    NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"badge" ascending:YES];
    NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor1,sortDescriptor2, nil];
    [fetchRequest setSortDescriptors:sortDescriptors];
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:@"badge" cacheName:nil];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;
    [aFetchedResultsController release];
    [fetchRequest release];
    [sortDescriptor1 release];
    [sortDescriptor2 release];
    [sortDescriptors release];

    [self.tableView reloadData];
    [super viewWillAppear:animated];
}

- (void)viewDidLoad:(BOOL)animated {
    [super viewDidLoad:animated];
}

答案 1 :(得分:0)

您是否正确实施了NSFetchedResultsControllerDelegate方法? FRC应该在收到来自托管对象上下文的更改通知时处理更新表...

修改: NSFetchedResultsController Class Reference