UITableView 不会刷新从核心数据中获取的结果

Posted

技术标签:

【中文标题】UITableView 不会刷新从核心数据中获取的结果【英文标题】:UITableView doesn't refresh fetched results from core data 【发布时间】:2011-03-28 01:12:16 【问题描述】:

大家好,我有一个 UISegmented 控制器,我试图用它在从核心数据获取的结果之间切换。但是,当分段控制器发生变化时,它不会更新 UITableView 中获取的结果。我不确定我做错了什么。希望有人能指出我正确的方向。这是我的代码:

- (void)viewDidLoad 
NameAppDelegate *theDelegate = (NameAppDelegate*)[[UIApplication sharedApplication] delegate];
    self.managedObjectContext = theDelegate.managedObjectContext;
    NSError *error = nil;
    if (![[self fetchedResultsController] performFetch:&error]) 
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    
    [super viewDidLoad];


- (IBAction)segmentedControlValueChanged 
    selectedUnit = tableSeg.selectedSegmentIndex;
    [self.theTableView reloadData];


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 

    if (selectedUnit == 0) 
    return [[fetchedResultsController sections] count];
    

            if (selectedUnit == 1) 
                return [[fetchedResultsController2 sections] count];
            
    return 0;



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    if (selectedUnit == 0) 
    id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
    return [sectionInfo numberOfObjects];
    
    if (selectedUnit == 1) 
        id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController2 sections] objectAtIndex:section];
        return [sectionInfo numberOfObjects];
    
    return 0;



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    if (selectedUnit == 0) 
    NSManagedObject *managedObject = [fetchedResultsController objectAtIndexPath:indexPath];
    static NSString *cellID = @"nameCell";

    NamesViewCustomCell *cell = (NamesViewCustomCell*)[tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) 
        NSLog(@"New Cell Made");

        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"NamesViewCustomCell" owner:nil options:nil];
        for(id currentObject in topLevelObjects)
        
            if([currentObject isKindOfClass:[NamesViewCustomCell class]])
            
                cell = (NamesViewCustomCell *)currentObject;
                break;
            
        
    
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterLongStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];

    // Configure the cell.
    [[cell nameName2] setText:[managedObject valueForKey:@"nameContent"]];

    return cell;
    
    if (selectedUnit == 1) 
        NSManagedObject *managedObject = [fetchedResultsController2 objectAtIndexPath:indexPath];
        static NSString *CellIdentifier = @"Cell";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) 
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        

        // Configure the cell.
        cell.textLabel.text = [managedObject valueForKey:@"nameContent"];

        return cell;
    
    return 0;



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 

    if (selectedUnit == 0) 
    RecipeDetailViewController *recipeDetailView = [[RecipeDetailViewController alloc] initWithStyle:UITableViewStyleGrouped];
    AddName *addName = (AddName *)[fetchedResultsController objectAtIndexPath:indexPath];
    recipeDetailView.addName = addName;
    [self.navigationController pushViewController:recipeDetailView animated:YES];
    
    if (selectedUnit == 1) 
        RecipeDetailViewController *recipeDetailView = [[RecipeDetailViewController alloc] initWithStyle:UITableViewStyleGrouped];
        AddName2 *addName2 = (AddName2 *)[fetchedResultsController2 objectAtIndexPath:indexPath];
        recipeDetailView.addName2 = addName2;
        [self.navigationController pushViewController:recipeDetailView animated:YES];
    

- (NSFetchedResultsController *)fetchedResultsController 

    if (selectedUnit == 0) 

        if (fetchedResultsController) return fetchedResultsController;

        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        NSEntityDescription *entity = 
        [NSEntityDescription entityForName:@"AddName" 
                    inManagedObjectContext:managedObjectContext];

        [fetchRequest setEntity:entity];

        NSSortDescriptor *sortDescriptor = 
        [[NSSortDescriptor alloc] initWithKey:@"timeStamp" 
                                    ascending:YES];

        NSArray *sortDescriptors = [[NSArray alloc] 
                                    initWithObjects:sortDescriptor, nil];  
        [fetchRequest setSortDescriptors:sortDescriptors];

        NSFetchedResultsController *aFetchedResultsController = 
        [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest 
                                            managedObjectContext:managedObjectContext
                                              sectionNameKeyPath:nil cacheName:@"cache"];
        aFetchedResultsController.delegate = self;
        [self setFetchedResultsController:aFetchedResultsController];

        [aFetchedResultsController release];
        [fetchRequest release];
        [sortDescriptor release];
        [sortDescriptors release];

        return fetchedResultsController;

    
    if (selectedUnit == 1) 

        if (fetchedResultsController2 != nil) 
            return fetchedResultsController2;
        


        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

        NSEntityDescription *entity = [NSEntityDescription entityForName:@"AddName2" inManagedObjectContext:managedObjectContext];
        [fetchRequest setEntity:entity];

        [fetchRequest setFetchBatchSize:20];

        NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timeStamp" ascending:NO];
        NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

        [fetchRequest setSortDescriptors:sortDescriptors];

        NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@"cache2"];
        aFetchedResultsController.delegate = self;
        self.fetchedResultsController2 = aFetchedResultsController;

        [aFetchedResultsController release];
        [fetchRequest release];
        [sortDescriptor release];
        [sortDescriptors release];
        return fetchedResultsController2;

    

return 0;

【问题讨论】:

您是否已将self.theTableView 连接到表格视图?和tableSeg 到触发事件的分段控件?在segmentedControlValueChanged 中添加NSLog(@"selection changed: %d %@",selectedUnit,self.theTableView); 以查看发生了什么。 一切正常,但它只是不获取结果。 tableSeg 有效,theTableView 有效。我可以在加载时获取结果,但是当我切换到另一个实体来获取这些结果时,它不会将它们加载到 tableview 中。它只是获取旧的结果。真的很奇怪。 【参考方案1】:

你需要把它放在你的- (IBAction)segmentedControlValueChanged

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

【讨论】:

以上是关于UITableView 不会刷新从核心数据中获取的结果的主要内容,如果未能解决你的问题,请参考以下文章

RSS提要核心数据保存在uitableview中

如何从核心数据中获取父实体的所有子实体,以及如何将父数据用作 UITableview

从数据源刷新 UITableView 数据的干净方法

UITableView 中的数据在特定情况下不会刷新

从 UITableViewCell 刷新 UITableView

刷新 UITableView tableFooterView 而不重新加载整个表数据