【问题标题】:UITableView repeating Firebase DataUITableView 重复 Firebase 数据
【发布时间】:2016-10-29 08:45:18
【问题描述】:

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

- (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)
         {
             NSString *username = snapshot.value[@"Name"];
             NSString *date = snapshot.value[@"Date"];
             NSString *combatPower = snapshot.value[@"Combat Power"];
             NSString *pokemon = snapshot.value[@"Pokemon"];
             NSString *pokemonURL = snapshot.value[@"Pokemon Image"];
             NSString *picURL = snapshot.value[@"Profile 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;
}

【问题讨论】:

  • 不要像这样直接使用firebase数据将其存储在一个数组中并访问它
  • 我不确定如何将所有这些值存储到 NSArray 中,然后将它们拉取...
  • 当你得到响应然后创建一个数组给你的快照和表 cellrowatindex 的值只是访问你的 nsarray
  • 对不起,你能给我举个小例子来说明你的意思吗?
  • 那是 Swift 代码吗?

标签: ios objective-c uitableview firebase firebase-realtime-database


【解决方案1】:

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

所以,将您的 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)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section      
{
    return [self.allSnapshots count];
}

cellForRowAtIndex:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)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[@"Pokemon"];
    NSString *pokemonURL = snapshot.value[@"Pokemon Image"];
    NSString *picURL = snapshot.value[@"Profile 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;
}

【讨论】:

  • 兄弟告诉我,在你的代码中 allSnapshots 类型?
  • 我告诉他这个概念,但他听不懂
  • FIRDataSnapshot* ?您正在用 Swift 回答一个 Objective-C 问题,所以这可能就是问题所在。我明白了您试图提及的内容,但需要更好的解释。
  • 我将“allSnapshots”设为 FIRDataSnapShot 的一个属性,按照您所说的做了,但它在 [self.allSnapshots.children objectAtIndex:indexPath.row]; 上抛出了错误
  • 可能有更好的方法,但我已经编辑了答案以将所有子快照添加到数组中,因此您现在可以直接访问它。 (self.allSnapshots 现在是 NSMutableArray*)
猜你喜欢
  • 1970-01-01
  • 2017-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多