ios - 如何使用 Firebase 在 UITableViewController 中正确显示数据?
<p><p>我正在构建一个类似于“instagram”的应用程序,其中包含带有图片的帖子。我正在使用 Firebase 存储/检索数据。 </p>
<p>我的问题出现是因为帖子以随机方式显示。例如,在 <code>cellForRowAtIndexPath</code> 中,第一个帖子显示在 indexPath,下一个帖子是 ,但下一个应该在 的帖子显示在 .</p>
<p>我将复制粘贴与该问题最相关的代码:</p>
<p>这是我检索分数的类(class):</p>
<pre><code>////// Arrays to store the data
nameArray = [init];
scoreArray = [init];
photomealArray = [init];
keysArray = [init]; // Reference to know what post should be scored.
// Reference to the Database
self.storageRef = [ reference];
FIRDatabaseReference *rootRef= [ referenceFromURL:@"https://XXXXXXX.firebaseio.com/"];
// Query all 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
[ observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull Usersnapshot) {
if(Usersnapshot.exists){
NSDictionary *Userdict = Usersnapshot.value;
for (NSString *Userkey in Userdict) {
NSString *Users_UserID = valueForKey:@"UserID"];
NSString *UserID = valueForKey:@"UserID"];
// If the userID matches with the person who posts, then add it to the array
if() valueForKey:@"Name"]]];
}
}
}];
UIImage *mealPhoto = valueForKey:@"PostPhoto"]]]];
NSString *Score = valueForKey:@"Score"];
NSString *PostKeys = valueForKey:@"KeyforPost"];
if() ; else ;
;
;
}
}
} withCancelBlock:^(NSError * _Nonnull error) {
NSLog(@"%@", error.localizedDescription);
}];
</code></pre>
<p>如果我退出并重新启动应用程序,新帖子将存储在从 开始的任何“空”索引中,这是预期的。 </p>
<p>这就是 <code>didSelectRowAtIndexPath</code> 的样子:</p>
<pre><code>-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{
FeedTableViewCell *cell =;
if(cell.selectionStyle != UITableViewCellSelectionStyleNone){
if( >= -1 && <= 1 && !){
NSString *keyforcell = ;
NSLog(@"key a usar :%@", keyforcell);
ref = [ referenceFromURL:@"https://XXXXX.firebaseio.com"];
[[[ child:keyforcell] child:@"Score"] setValue:]];
[[[ child:keyforcell] child:@"Rated"] setValue:@"YES"];
] atIndex:indexPath.row];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
}
</code></pre>
<p>我曾尝试使用 <code>observeSingleEventOfType</code> 和 <code>observeEventType</code>,因为这可能与单个仅调用一次这一事实有关。另外我认为在选择单元格后会触发 <code>viewDidLoad()</code> ,但它不应该。另外,我认为它与 NSMutableArrays 有关,但我不完全确定</p>
<p>谁能帮帮我? </p>
<p><strong>更新</strong>:我知道 Firebase 和 NSDictionary 都不会返回/存储排序数据,所以我的猜测是我的问题与排序项目有关。我了解了 <code>orderbykey</code> 和 <code>orderbyvalue</code> 但它们似乎不起作用。 </p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>不清楚您的问题是什么,但您不应忘记 Firebase 数据库中没有数组。唯一的集合类型是字典,字典没有键顺序。 </p>
<p>您应该考虑对对象使用排序系统,而不是直接将它们添加到数组中。</p></p>
<p style="font-size: 20px;">关于ios - 如何使用 Firebase 在 UITableViewController 中正确显示数据?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/47209289/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/47209289/
</a>
</p>
页:
[1]