UITableView重复Firebase数据 [英] UITableView repeating Firebase Data

查看:163
本文介绍了UITableView重复Firebase数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在重复Firebase的内容,而我似乎无法弄清楚我做错了什么。在firebase我有6个职位。 tableview是填充6个单元格,但所有6个单元格具有相同的数据,其他5个职位不在那里。

   - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

{
RankingsCell * cell = [tableView dequeueReusableCellWithIdentifier:@RankingsCell];
self.ref = [[FIRDatabase database] reference];
posts = [ref child:@posts];
$ b $ posts $(b
$ snapshot $ snapshot $ $









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);
}];
返回单元格;

$ / code>


解决方案

cellForRowAtIndex:方法被称为每个单元,所以你不应该在那里做你的数据库工作,它只是负责一次创建一个单元格。 b
$ b

因此,在 viewDidLoad:或<$ c中调用 observeEventType:
$ b

   - (void)viewDidLoad 
{$ b> viewDidAppear:
$ b [super viewDidLoad];

self.ref = [[FIRDatabase database] reference];
posts = [ref child:@posts];

[posts observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * snapshot)
{
self.allSnapshots = [NSMutableArray array];

(snapshot.children中的快照)
{
[self.allSnapshots addObject:snapshot];
}

[self.tableView reloadData]; //获取数据后刷新表视图
} // ..错误...
}

并在 numberOfRowsForInSection:

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

$ / code>

cellForRowAtIndex: {$($ {$ p
$ b $ pre
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;
}


I'm getting repeated content from Firebase, and I can't seem to figure out what I am doing wrong. In firebase I have 6 posts. The tableview is populating 6 cells, but all 6 cells have the same data, and the 5 other posts are not there.

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

解决方案

cellForRowAtIndex: method is called for "every" cell, so you're not supposed to do your database work in there, it's just responsible for creating "one" cell at a time.

So, move your observeEventType: call in viewDidLoad: or 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 ...
}

And in numberOfRowsForInSection:

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

And in 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;
}

这篇关于UITableView重复Firebase数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆