菜鸟教程小白 发表于 2022-12-11 19:13:25

ios - 将两个从两个独立的firebase数据库加载到一个tableview swift中


                                            <p><p>我正在构建一个非常简单的应用程序,人们在其中单击一个按钮,并使用 firebase 数据库记录他们单击按钮( checkin )的时间。我没有问题<strong>存储</strong>签到人员的数据,然后显示签到人员的数据以及所有适当的信息。我已经包含了一张图片来显示我已经在我的 TableViewController 中显示的内容。
<a href="/image/nyr7I.png" rel="noreferrer noopener nofollow"><img src="/image/nyr7I.png" alt="This is what my TableView shows right now"/></a> </p>

<p>我不想只显示已 checkin 的用户我还想显示尚未 checkin 的用户。</p>

<p>我现在将分享一张照片,说明我的 firebase 数据库是如何设置为存储每个用户的。 </p>

<p> <a href="/image/Mrf9T.png" rel="noreferrer noopener nofollow"><img src="/image/Mrf9T.png" alt="How my database is setup to store all users"/></a> </p>

<p>这是整个数据库的 View ,其中显示了用户和已经 checkin 的用户以及名称和时间戳</p>

<p> <a href="/image/iViCw.png" rel="noreferrer noopener nofollow"><img src="/image/iViCw.png" alt="Entire database"/></a> </p>

<p>我还将包含我的 UITableViewController 中的代码,这样每个人都可以看到我如何只显示已经签到的人。</p>

<pre><code> import Firebase

struct checkInStruct {

let userName : String!
let hour : String!
let minutes : String!

}

class checkInDaysTableViewController: UITableViewController {


//Variables
var lastChildNameSegue = &#34;&#34;

var posts = ()
let user = FIRAuth.auth()?.currentUser

override func viewDidLoad() {
    super.viewDidLoad()

    let databaseRef = FIRDatabase.database().reference()

    let userID = user?.uid

    databaseRef.child(&#34;users&#34;).child(&#34;\(userID!)&#34;).child(&#34;checkins&#34;).child(&#34;\(lastChildNameSegue)&#34;).queryOrderedByKey().observe(.childAdded, with: { (snapshot) in


      let userNameSnapValue = snapshot.value as? NSDictionary
      let userName = userNameSnapValue?[&#34;userName&#34;] as? String


      let hourSnapValue = snapshot.value as? NSDictionary
      let hour = hourSnapValue?[&#34;hour&#34;] as? String

      let minutesValue = snapshot.value as? NSDictionary
      let minutes = minutesValue?[&#34;minutes&#34;] as? String

      print(userName!)
      print(hour!)
      print(minutes!)

      self.posts.insert(checkInStruct(userName: userName, hour: hour, minutes: minutes) , at: 0)
      self.tableView.reloadData()


    })

}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
    return posts.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: &#34;Cell&#34;)

    let userName = posts.userName
    let hour = posts.hour
    var minute = posts.minutes

    let minuteInt = Int(minute!)

    if(minuteInt! &gt;= 0 &amp;&amp; minuteInt! &lt;= 9){
      let minuteWithZero = &#34;0\(minuteInt!)&#34;
      minute = minuteWithZero
    }

    let label1 = cell?.viewWithTag(1) as! UILabel
    label1.text = &#34;\(userName!)&#34;

    let label2 = cell?.viewWithTag(2) as! UILabel
    label2.text = &#34;Checked In At \(hour!):\(minute!)&#34;

    return cell!
}



}
</code></pre>

<p><strong>我需要做什么才能加载所有用户的数据并以红色突出显示尚未签到的任何人?</strong></p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我认为你应该这样设计你的数据库。</p>

<p><code>root -> 用户</code></p>

<pre><code>&#34;users&#34;: [
    &#34;{id_user_A}&#34;: {
      &#34;name&#34;: A,
      &#34;checkedin&#34;: [
            &#34;{id_checkedin_1}&#34;: {
               ...
             },
            &#34;{id_checkedin_2}&#34;: {
               ...
             }
      ]
    },
    &#34;{id_user_B}&#34;: {
      &#34;name&#34;: B,
      &#34;checkedin&#34;: []
   }
]
</code></pre>

<p>并获取所有用户,使用 <code>checkedin</code> 属性检查每个用户以了解哪个用户 checkin 。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 将两个从两个独立的firebase数据库加载到一个tableview swift中,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/42730405/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/42730405/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 将两个从两个独立的firebase数据库加载到一个tableview swift中