gpt4 book ai didi

ios - UITableView 重复 Firebase 数据

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:27:32 24 4
gpt4 key购买 nike

我从 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"];

[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: 方法为“每个”单元格调用,因此您不应该在其中进行数据库工作,它只负责一次创建“一个”单元格。

因此,将您的 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;
}

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com