[Rà‰SOLU] Ajouter un objet dans CoreData à  un index spécifique

BenjoBenjo Membre
août 2013 modifié dans API UIKit #1

Bonjour à  tous


 


J'ai un petit soucis avec l'ajout d'un objet dans CoreData. Je dois ajouter un objet de type NSString dans ma base de données mais à  un index précis.


Disons que j'ai 5 objets dans ma base de données. Je dois ajouter mon 6ème objet à  l'index 0 donc juste avant le premier objet. Seulement, je ne sais pas comment trouver l'index. Voici mon code pour ajouter :



-(void)ajouterAIndex:(int)index nombre:(NSString *)nombre {
// On ajoute
Pile *pile = [NSEntityDescription insertNewObjectForEntityForName:@Pile inManagedObjectContext:self.managedObjectContext];

// Attributs
pile.nombre = nombre;

NSError *error;
[self.managedObjectContext save:&error];

// On ajoute l'objet à  tableauPile
tableauPile = [tableauPile arrayByAddingObject:pile];

// On insert un rang à  tableau
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:index inSection:0];
[tableau insertRowsAtIndexPaths:@[;indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

Est-ce que le problème ne viendrait-il pas de mon "tableauPile = [tableauPile arrayByAddingObject:pile];" ?


 


Quelqu'un a-t-il une idée sur la question ?


 


Merci d'avance :)


Mots clés:

Réponses

  • Bonsoir,


     


    Déclare plus tôt ton tableauPile en MutableArray et utilise la méthode d'insertion : 



    - (void)insertObject:(id)anObject atIndex:(NSUInteger)index
  • BenjoBenjo Membre
    juillet 2013 modifié #3

    Merci samir2303 pour ta réponse rapide. Donc mon problème vient bien de là .


    J'ai essayé ta solution. Voici mon code désormais :



    -(void)ajouterAIndex:(int)index nombre:(NSString *)nombre {
    // On ajoute
    Pile *pile = [NSEntityDescription insertNewObjectForEntityForName:@Pile inManagedObjectContext:self.managedObjectContext];

    // Attributs
    pile.nombre = nombre;

    NSError *error;
    [self.managedObjectContext save:&error];

    // On ajoute l'objet à  tableauPile
    [tableauPile insertObject:pile atIndex:index];

    // On insert un rang à  tableau
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:index inSection:0];
    [tableau insertRowsAtIndexPaths:@[;indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }

    Mais j'ai un crash de l'application :


    "*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_PFArray insertObject:atIndex:]: unrecognized selector sent to instance 0x748ede0'


    ***"


    Je ne vois vraiment pas ce qui ne va pas...


  • Comment tu déclare tableauPile ? il faut le déclarer en NSMutableArray et il faut l'initialiser aussi.


  • C'est bon j'ai trouvé l'erreur j'avais mal initialisé mon NSMuttableArray. Merci samir2303.


Connectez-vous ou Inscrivez-vous pour répondre.