OGeek|极客世界-中国程序员成长平台

标题: ios - UITableView 重复 Firebase 数据 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 22:46
标题: ios - UITableView 重复 Firebase 数据

我从 Firebase 收到重复的内容,但我似乎无法弄清楚我做错了什么。在 firebase 我有 6 个帖子。 tableview 正在填充 6 个单元格,但所有 6 个单元格都具有相同的数据,并且其他 5 个帖子不存在。

- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)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)
         {
             NSString *username = snapshot.value[@"Name"];
             NSString *date = snapshot.value[@"Date"];
             NSString *combatPower = snapshot.value[@"Combat Power"];
             NSString *pokemon = snapshot.value[@"okemon"];
             NSString *pokemonURL = snapshot.value[@"okemon Image"];
             NSString *picURL = snapshot.value[@"rofile Picture"];
             int CP = [combatPower intValue];

             cell.usernameOutlet.text = username;
             cell.dateOutlet.text = date;
             cell.combatPowerLabel.text = [NSString stringWithFormat"COMBAT POWER: %d", CP];
             cell.pokemonLabel.text = pokemon;
             [cell downloadUserImage:picURL];
             [cell downloadPokemonImage:pokemonURL];
         }
     }
        withCancelBlock:^(NSError * _Nonnull error)
     {
         NSLog(@"%@", error.localizedDescription);
     }];
    return cell;
}



Best Answer-推荐答案


cellForRowAtIndex: 方法为“每个”单元格调用,因此您不应该在那里进行数据库工作,它只负责一次创建“一个”单元格。 p>

因此,将您的 observeEventType: 调用移动到 viewDidLoad:viewDidAppear: 中,例如:

- (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 ...
}

而在numberOfRowsForInSection:

- (NSInteger)tableViewUITableView *)tableView numberOfRowsInSectionNSInteger)section      
{
    return [self.allSnapshots count];
}

而在 cellForRowAtIndex:

- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath
{
    RankingsCell *cell = [tableView dequeueReusableCellWithIdentifier"RankingsCell"];

    FIRDataSnapshot *snapshot = [self.allSnapshots objectAtIndex:indexPath.row]; 

    NSString *username = snapshot.value[@"Name"];
    NSString *date = snapshot.value[@"Date"];
    NSString *combatPower = snapshot.value[@"Combat Power"];
    NSString *pokemon = snapshot.value[@"okemon"];
    NSString *pokemonURL = snapshot.value[@"okemon Image"];
    NSString *picURL = snapshot.value[@"rofile Picture"];
    int CP = [combatPower intValue];

    cell.usernameOutlet.text = username;
    cell.dateOutlet.text = date;
    cell.combatPowerLabel.text = [NSString stringWithFormat"COMBAT POWER: %d", CP];
    cell.pokemonLabel.text = pokemon;
    [cell downloadUserImage:picURL];
    [cell downloadPokemonImage:pokemonURL];

    return cell;
}

关于ios - UITableView 重复 Firebase 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40317448/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4