开发者

UITableView doesn't refresh fetched results from core data

开发者 https://www.devze.com 2023-02-19 23:17 出处:网络
Hey guys, I have a UISegmented controller that I\'m trying to use to switch between fetched results from core data. However, when the segmented controller changes it doesn\'t update the fetched result

Hey guys, I have a UISegmented controller that I'm trying to use to switch between fetched results from core data. However, when the segmented controller changes it doesn't update the fetched results in the UITableView. I'm not sure what I'm doing wrong. Hopefully someone can point me in the right direction. Here is my code:

- (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 dequeueReusableCellWi开发者_StackOverflow中文版thIdentifier: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;

}


You need to put this in your - (IBAction)segmentedControlValueChanged

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号