使用Firebase数据库中的数据填充表视图时遇到一些问题。我的NSMutableArray在块内始终为空。如何用从数据库中获取的数据填充tableView?
仅目标C
到目前为止,我已经以这种方式实现了代码
@interface UserSearchController ()
@property (nonatomic, strong) NSMutableArray *users;
@end
@implementation UserSearchController
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cellID"];
[self retrieveAllUser];
}
-(void)retrieveAllUser {
FIRDatabaseReference *dbRef = FIRDatabase.database.reference;
_users = NSMutableArray.new;
[[[dbRef child:kUserAccFolder] queryOrderedByChild:kUserAccEmail]
observeEventType:FIRDataEventTypeChildAdded withBlock:^(FIRDataSnapshot *snapshot) {
[self.users addObject:snapshot.value[kUserAccName]];
[self.tableView reloadData];
}];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _users.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID" forIndexPath:indexPath];
cell.textLabel.text = [NSString stringWithFormat:@"%@",self.users[indexPath.row]];
return cell;
}