UITableView Répétition des données Firebase

Je reçois du contenu répété de Firebase, et je n'arrive pas à comprendre ce que je fais de mal. En firebase j'ai 6 posts. La vue de table est peupler 6 cellules, mais toutes les 6 cellules ont les mêmes données, et les 5 autres posts ne sont pas là.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { RankingsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RankingsCell"]; self.ref = [[FIRDatabase database] reference]; posts = [ref child:@"posts"]; [posts observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) { for (snapshot in snapshot.children) { NSSsortingng *username = snapshot.value[@"Name"]; NSSsortingng *date = snapshot.value[@"Date"]; NSSsortingng *combatPower = snapshot.value[@"Combat Power"]; NSSsortingng *pokemon = snapshot.value[@"Pokemon"]; NSSsortingng *pokemonURL = snapshot.value[@"Pokemon Image"]; NSSsortingng *picURL = snapshot.value[@"Profile Picture"]; int CP = [combatPower intValue]; cell.usernameOutlet.text = username; cell.dateOutlet.text = date; cell.combatPowerLabel.text = [NSSsortingng ssortingngWithFormat:@"COMBAT POWER: %d", CP]; cell.pokemonLabel.text = pokemon; [cell downloadUserImage:picURL]; [cell downloadPokemonImage:pokemonURL]; } } withCancelBlock:^(NSError * _Nonnull error) { NSLog(@"%@", error.localizedDescription); }]; return cell; } 

cellForRowAtIndex: méthode est appelée pour "chaque" cellule, donc vous n'êtes pas censé faire fonctionner votre database, il est juste responsable de créer "une" cellule à la fois.

Donc, déplacez votre observeEventType: appelez viewDidLoad: ou viewDidAppear: like:

 - (void)viewDidLoad { [super viewDidLoad]; self.ref = [[FIRDatabase database] reference]; posts = [ref child:@"posts"]; [posts observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) { self.allSnapshots = [NSMutableArray array]; for (snapshot in snapshot.children) { [self.allSnapshots addObject:snapshot]; } [self.tableView reloadData]; // Refresh table view after getting data } // .. error ... } 

Et dans numberOfRowsForInSection:

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [self.allSnapshots count]; } 

Et dans cellForRowAtIndex:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { RankingsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RankingsCell"]; FIRDataSnapshot *snapshot = [self.allSnapshots objectAtIndex:indexPath.row]; NSSsortingng *username = snapshot.value[@"Name"]; NSSsortingng *date = snapshot.value[@"Date"]; NSSsortingng *combatPower = snapshot.value[@"Combat Power"]; NSSsortingng *pokemon = snapshot.value[@"Pokemon"]; NSSsortingng *pokemonURL = snapshot.value[@"Pokemon Image"]; NSSsortingng *picURL = snapshot.value[@"Profile Picture"]; int CP = [combatPower intValue]; cell.usernameOutlet.text = username; cell.dateOutlet.text = date; cell.combatPowerLabel.text = [NSSsortingng ssortingngWithFormat:@"COMBAT POWER: %d", CP]; cell.pokemonLabel.text = pokemon; [cell downloadUserImage:picURL]; [cell downloadPokemonImage:pokemonURL]; return cell; }