Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/105.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
来自多个节点的iOS Firebase Objective C UITableView_Ios_Objective C_Uitableview_Firebase_Firebase Realtime Database - Fatal编程技术网

来自多个节点的iOS Firebase Objective C UITableView

来自多个节点的iOS Firebase Objective C UITableView,ios,objective-c,uitableview,firebase,firebase-realtime-database,Ios,Objective C,Uitableview,Firebase,Firebase Realtime Database,我一直在使用firebase友好聊天示例将单个节点中的新闻项列表读取到UITableView中,效果良好 但是,我现在想从两个节点读取数据:- 1) 。首先读入新闻节点中的所有项目 2) 。然后根据当前登录的用户,突出显示他们喜欢的新闻项目 下面是我的代码和firebase数据库结构,我的问题是实现这一点的最佳方法是什么?我是否将新项目读取到数组中,然后根据登录的用户读取收藏夹,如果不是null,则将它们附加到数组中 有什么想法是实现这一目标的最佳方式吗?谢谢 火基结构 "news": { "o

我一直在使用firebase友好聊天示例将单个节点中的新闻项列表读取到UITableView中,效果良好

但是,我现在想从两个节点读取数据:-

1) 。首先读入新闻节点中的所有项目

2) 。然后根据当前登录的用户,突出显示他们喜欢的新闻项目

下面是我的代码和firebase数据库结构,我的问题是实现这一点的最佳方法是什么?我是否将新项目读取到数组中,然后根据登录的用户读取收藏夹,如果不是null,则将它们附加到数组中

有什么想法是实现这一目标的最佳方式吗?谢谢

火基结构

"news": {
"one": {
  "name": "Sample New Item",
  "icon": "1.png",
  "timestamp": 1459361875666
},
"two": { ... },
"three": { ... }
},

"users": {
"DZ9sBWCbcbevdnjV3hRcwKq1Urg1": {
"one": {
  "favourite": true
},
"five": {
  "favourite": true
},
"DZ9sBWCbcbevdnjV3hRcwKq1HSrg3": {
"six": {
  "favourite": true
},
代码

self.ref=[[FIRDatabase数据库]参考];
newsArray=[[NSMutableArray alloc]init];
_refHandle=[[ref child:@“news”]observeEventType:FIRDataEventTypeChildAdded with block:^(FIRDataSnapshot*快照)
{
[新闻数组添加对象:快照];
[tableView InsertRowsAndExpaths:@[[NSIndexPath indexPathForRow:newsArray.count-1第1节:0]]带RowAnimation:UITableViewRowAnimationTop];
NSLog(@“%@”,快照);
}];
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
{
返回[新闻数组计数];
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:@“cell”];
FIRDataSnapshot*newsSnapshot=newsArray[indexath.row];
NSDictionary*news=newsnapshot.value;
UILabel*新闻标签=(UILabel*)[带标签的单元格视图:100];
areaLabel.text=新闻[@“名称”];
UIImageView*单元格图标=(UIImageView*)[带标记的单元格视图:200];
cellIcon.image=[UIImage ImageName:news[@“icon”];
返回单元;

}

star应该是针对当前登录用户的吗?尽管我可以看到您有很多更好的方法来实现上述目标,但请查看firebase iOS Objective C示例,您将在那里看到一个用户、帖子、启动等,这正是您需要的,男士:)。是的,我只想在当前登录用户的新闻项旁边显示star。我可以获取当前的用户ID并提取他们喜欢的所有新闻项目,但我不知道最好的方法是组合来自两个节点(两个快照)的数据并将其显示在tableView中。
self.ref = [[FIRDatabase database] reference];

newsArray = [[NSMutableArray alloc] init];

_refHandle = [[ref child:@"news"] observeEventType:FIRDataEventTypeChildAdded withBlock:^(FIRDataSnapshot *snapshot)
              {
                  [newsArray addObject:snapshot];

                  [tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:newsArray.count-1 inSection:0]] withRowAnimation:UITableViewRowAnimationTop];
                  NSLog(@"%@", snapshot);
              }];
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];

FIRDataSnapshot *newsSnapshot = newsArray[indexPath.row];
NSDictionary<NSString *, NSString *> *news = newsSnapshot.value;

UILabel *newsLabel = (UILabel *)[cell viewWithTag:100];
areaLabel.text = news[@"name"];

UIImageView *cellIcon = (UIImageView *)[cell viewWithTag:200];
cellIcon.image = [UIImage imageNamed:news[@"icon"]];

return cell;