OGeek|极客世界-中国程序员成长平台

标题: ios - 将两个从两个独立的firebase数据库加载到一个tableview swift中 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:13
标题: ios - 将两个从两个独立的firebase数据库加载到一个tableview swift中

我正在构建一个非常简单的应用程序,人们在其中单击一个按钮,并使用 firebase 数据库记录他们单击按钮( checkin )的时间。我没有问题存储签到人员的数据,然后显示签到人员的数据以及所有适当的信息。我已经包含了一张图片来显示我已经在我的 TableViewController 中显示的内容。 This is what my TableView shows right now

我不想只显示已 checkin 的用户我还想显示尚未 checkin 的用户。

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

How my database is setup to store all users

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

Entire database

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

 import Firebase

struct checkInStruct {

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

}

class checkInDaysTableViewController: UITableViewController {


//Variables
var lastChildNameSegue = ""

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

override func viewDidLoad() {
    super.viewDidLoad()

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

    let userID = user?.uid

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


        let userNameSnapValue = snapshot.value as? NSDictionary
        let userName = userNameSnapValue?["userName"] as? String


        let hourSnapValue = snapshot.value as? NSDictionary
        let hour = hourSnapValue?["hour"] as? String

        let minutesValue = snapshot.value as? NSDictionary
        let minutes = minutesValue?["minutes"] 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) -> Int {
    return posts.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")

    let userName = posts[indexPath.row].userName
    let hour = posts[indexPath.row].hour
    var minute = posts[indexPath.row].minutes

    let minuteInt = Int(minute!)

    if(minuteInt! >= 0 && minuteInt! <= 9){
        let minuteWithZero = "0\(minuteInt!)"
        minute = minuteWithZero
    }

    let label1 = cell?.viewWithTag(1) as! UILabel
    label1.text = "\(userName!)"

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

    return cell!
}



}

我需要做什么才能加载所有用户的数据并以红色突出显示尚未签到的任何人?



Best Answer-推荐答案


我认为你应该这样设计你的数据库。

root -> 用户

"users": [
    "{id_user_A}": {
        "name": A,
        "checkedin": [
            "{id_checkedin_1}": {
               ...
             },
            "{id_checkedin_2}": {
               ...
             } 
        ]
    },
    "{id_user_B}": {
        "name": B,
        "checkedin": []
     }
]

并获取所有用户,使用 checkedin 属性检查每个用户以了解哪个用户 checkin 。

关于ios - 将两个从两个独立的firebase数据库加载到一个tableview swift中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42730405/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4