【发布时间】:2017-11-09 18:40:08
【问题描述】:
我正在构建一个类似于“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,但它们似乎不起作用。
【问题讨论】:
标签: ios objective-c uitableview firebase firebase-realtime-database