我正在构建类似“instagram”应用程序的东西,其中包含带图片的帖子。我正在使用 Firebase 来存储/检索数据。
我的问题是因为帖子以随机方式显示。例如,在 cellForRowAtIndexPath
中,第一篇文章显示在 indexPath[0],下一篇文章是 [1],但下一篇应该在 [2],它显示在 [0] .
我将复制粘贴与该问题最相关的代码:
这是我检索分数的类:
////// Arrays to store the data
nameArray = [[NSMutableArray alloc]init];
scoreArray = [[NSMutableArray alloc]init];
photomealArray = [[NSMutableArray alloc]init];
keysArray = [[NSMutableArray alloc]init]; // Reference to know what post should be scored.
// Reference to the Database
self.storageRef = [[FIRStorage storage] reference];
FIRDatabaseReference *rootRef= [[FIRDatabase database] referenceFromURL:@"https://XXXXXXX.firebaseio.com/"];
// Query all posts
[[rootRef child:@"Posts"] observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {
if(snapshot.exists){
NSDictionary *dict = snapshot.value;
for (NSString *key in dict) {
// Query all users to show the Name of the person who posts
[[rootRef child:@"Users"] observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull Usersnapshot) {
if(Usersnapshot.exists){
NSDictionary *Userdict = Usersnapshot.value;
for (NSString *Userkey in Userdict) {
NSString *Users_UserID = [Userdict[Userkey] valueForKey:@"UserID"];
NSString *UserID = [dict[key] valueForKey:@"UserID"];
// If the userID matches with the person who posts, then add it to the array
if([Users_UserID isEqualToString:UserID]) [nameArray addObject:[NSString stringWithFormat:@"%@",[Userdict[Userkey] valueForKey:@"Name"]]];
}
}
}];
UIImage *mealPhoto = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[dict[key] valueForKey:@"PostPhoto"]]]];
NSString *Score = [dict[key] valueForKey:@"Score"];
NSString *PostKeys = [dict[key] valueForKey:@"KeyforPost"];
if([Score isEqual:@"null"]) [scoreArray addObject:@"0"]; else [scoreArray addObject:Score];
[photomealArray addObject:mealPhoto];
[keysArray addObject:PostKeys];
}
}
} withCancelBlock:^(NSError * _Nonnull error) {
NSLog(@"%@", error.localizedDescription);
}];
如果我退出并重新启动应用程序,新帖子将存储在从 [0] 开始的任何“空”索引中,这是预期的。
这是 didSelectRowAtIndexPath
的样子:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{
FeedTableViewCell *cell =[tableView cellForRowAtIndexPath:indexPath];
if(cell.selectionStyle != UITableViewCellSelectionStyleNone){
if([score.text intValue] >= -1 && [score.text intValue] <= 1 && ![score.text isEqual: @""]){
NSString *keyforcell = [keysArray objectAtIndex:indexPath.row];
NSLog(@"key a usar :%@", keyforcell);
ref = [[FIRDatabase database] referenceFromURL:@"https://XXXXX.firebaseio.com"];
[[[[self.ref child:@"Posts"] child:keyforcell] child:@"Score"] setValue:[NSNumber numberWithInt:[score.text intValue]]];
[[[[self.ref child:@"Posts"] child:keyforcell] child:@"Rated"] setValue:@"YES"];
[scoreArray insertObject:[NSNumber numberWithInt:[score.text intValue]] atIndex:indexPath.row];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
}
我尝试使用 observeSingleEventOfType
和 observeEventType
因为这可能与单个事件仅被调用一次这一事实有关。我还认为 viewDidLoad()
在选择单元格后被触发,但它不应该。另外,我认为这与 NSMutableArrays 有关,但我不完全确定
谁能帮帮我?
更新:我知道 Firebase 和 NSDictionary 都不会返回/存储排序后的数据,所以我猜我的问题与排序项目有关。我开始了解 orderbykey
和 orderbyvalue
但它们似乎不起作用。
最佳答案
不清楚你的问题是什么,但你不应该忘记 Firebase 数据库中没有数组。唯一的集合类型是字典,字典没有键顺序。
您应该考虑对您的对象进行排序,而不是直接将它们添加到数组中。
关于ios - 如何使用 Firebase 在 UITableViewController 中正确显示数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47209289/