NSFetchedResultsController de l'indice au-delà des limites

Je suis en utilisant un NSFetchedResultsController pour afficher des éléments dans mon tableau: les

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    //Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    //Return the number of rows in the section.
    return [[self.fetchedResultsController fetchedObjects] count];
}


//Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"TagCell";

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

 Tag *tag = [self.fetchedResultsController objectAtIndexPath:indexPath];
 cell.textLabel.text = tag.name;

    return cell;
}

Toutefois, ce code s'arrête au niveau de cette ligne:

Tag *tag = [self.fetchedResultsController objectAtIndexPath:indexPath];

Avec ce message:

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFArray objectAtIndex:]: index (0) beyond bounds (0)'

J'ai NSLogged [self.fetchedResultsController fetchedObjects] et je peux confirmer qu'il y a en effet de marquer les objets. Si je remplace cette ligne ci-dessus par ce que tout fonctionne comme prévu:

Tag *tag = [[self.fetchedResultsController fetchedObjects] objectAtIndex:indexPath.row];

Je NSLogged indexPath et les index sont {0, 0} (section 0 en rangée 0), donc je sais que ce n'est pas un problème avec la section. Je suis extrêmement confus quant à pourquoi ce qui se passe parce que théoriquement, ces deux éléments de code pour faire la même chose. Toute aide est appréciée.

Grâce

MISES à jour:

id section = [[[self fetchedResultsController] sections] objectAtIndex:[indexPath section]];
NSLog(@"Section %@", section); <-- returns a valid section

Ce code entraîne la même exception: Tag *tag = [[section objects] objectAtIndex:[indexPath row];

Si je NSLog [section objects] elle renvoie un tableau vide. Je ne sais pas pourquoi [fetchedResultsController fetchedObjects] retourne un tableau avec les bons objets, et [section objects] ne retourne rien. Est-ce à dire que les objets que je crée ont pas de section? Voici le code que j'utilise pour ajouter de nouveaux objets:

- (void)newTagWithName:(NSString *)name
{
    NSIndexPath *currentSelection = [self.tableView indexPathForSelectedRow];
    if (currentSelection != nil) {
        [self.tableView deselectRowAtIndexPath:currentSelection animated:NO];
    }    

    NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
    Tag *newTag = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:self.managedObjectContext];

    //Configure new tag

    newTag.name = name;

    [self saveContext];

    NSIndexPath *rowPath = [self.fetchedResultsController indexPathForObject:newTag];
    [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:rowPath] withRowAnimation:UITableViewRowAnimationTop];
    [self.tableView selectRowAtIndexPath:rowPath animated:YES scrollPosition:UITableViewScrollPositionTop];
    [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
}

Et voici mon saveContext méthode:

- (void)saveContext
{
    //Save changes

    NSError *error;
    BOOL success = [self.managedObjectContext save:&error];
    if (!success)
    {
        UIAlertView *errorAlert = [[[UIAlertView alloc] initWithTitle:@"Error encountered while saving." message:nil delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil] autorelease];
        [errorAlert show];
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    }
}

Je fais quelque chose de mal ici?

InformationsquelleAutor indragie | 2010-07-03