tableView reloadData不工作 [英] tableView reloadData does not work
问题描述
我在标签栏应用程序中有一个tableview。
I have a tableview in a tab bar application.
我正在加载 viewDidLoad
p>
I am loading the data in 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,它不更新。更新的唯一方法是如果我退出应用程序并从头重新加载。
In this view, I call another view, which when changed, updated the core data when released. When I navigate back to the tableview, it does not update. The only way it will update, is if I quit the app and reload from scratch.
总共有2个单独的表视图。奇怪的是,第一个tableview刷新。
In total there are 2 separate table views. The strange thing is, the first tableview DOES refresh.
任何想法?
Rob
Any thoughts? Rob
推荐答案
您需要执行您在中执行的操作viewDidLoad
在 viewWillAppear
。 viewDidLoad
仅在启动时调用,因此尽管更改了数据,但您的视图从不更改其本地数据,因此当您调用 reloadData
它仍然使用旧数据。
You need to do what you do in viewDidLoad
in viewWillAppear
. viewDidLoad
is only called at startup, so despite changing the data, your view never changes its own local data, so when you call reloadData
it still uses the old data.
编辑:
你应该有:
- (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];
}
这篇关于tableView reloadData不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!