UITableView重复Firebase数据 [英] UITableView repeating Firebase Data
问题描述
- (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 [super viewDidLoad];
{$ b> viewDidAppear:
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屋!