菜鸟教程小白 发表于 2022-12-11 19:15:53

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:@&#34;https://XXXXXXX.firebaseio.com/&#34;];


// 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:@&#34;UserID&#34;];
                        NSString *UserID = valueForKey:@&#34;UserID&#34;];
                        // If the userID matches with the person who posts, then add it to the array
                        if() valueForKey:@&#34;Name&#34;]]];

                  }
                }

            }];

            UIImage *mealPhoto = valueForKey:@&#34;PostPhoto&#34;]]]];

            NSString *Score = valueForKey:@&#34;Score&#34;];
            NSString *PostKeys = valueForKey:@&#34;KeyforPost&#34;];
            if() ; else ;
            ;
            ;

      }

    }
} withCancelBlock:^(NSError * _Nonnull error) {
    NSLog(@&#34;%@&#34;, 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( &gt;= -1 &amp;&amp; &lt;= 1 &amp;&amp; !){

            NSString *keyforcell = ;

            NSLog(@&#34;key a usar :%@&#34;, keyforcell);

             ref = [ referenceFromURL:@&#34;https://XXXXX.firebaseio.com&#34;];


            [[[ child:keyforcell] child:@&#34;Score&#34;] setValue:]];

            [[[ child:keyforcell] child:@&#34;Rated&#34;] setValue:@&#34;YES&#34;];

            ] 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]
查看完整版本: ios - 如何使用 Firebase 在 UITableViewController 中正确显示数据?