tableView reloadData does not work

后端 未结 2 883
栀梦
栀梦 2021-01-16 07:28

I have a tableview in a tab bar application.

I am loading the data in viewDidLoad

managedObjectContext = nil;
managedObjectContext = [(R         


        
2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-16 07:54

    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.

    Edit:

    What you should have:

    - (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];
    }
    

提交回复
热议问题