gpt4 book ai didi

ios - 从firebase数据库中检索数据到tableView中的类

转载 作者:行者123 更新时间:2023-11-30 11:46:23 26 4
gpt4 key购买 nike

我正在尝试从我的数据库中获取用户的数据。我需要在当前授权用户的 tableView 中显示所有数据。

现在我在 firebase 中有这个结构:

{
"users" : {
"fZjVIhQrqvcQYIa0CjfmDNnaUao2" : {
"email" : "email@email.email",
"historyBooking" : {
"-L5FGrgRULvn_E4GV9ht" : {
"bookingDate" : "13.02.2018",
"bookingTime" : "C 02:00 до 3:00 (итого 1 час.)",
"email" : "",
"name" : "",
"phonenumber" : "",
"studioHallAddress" : "AddressOne",
"studioHallName" : "HallOne",
"surename" : "",
"totalSum" : "1500 руб."
},
"-L5FO_GQSLixyRBtjZVt" : {
"bookingDate" : "14.02.2018",
"bookingTime" : "C 11:00 до 12:00 (итого 1 час.)",
"email" : "",
"name" : "",
"phonenumber" : "",
"studioHallAddress" : "AddressTwo",
"studioHallName" : "HallTwo",
"surename" : "",
"totalSum" : "1900 руб."
}
},
"uid" : "fZjVIhQrqvcQYIa0CjfmDNnaUao2",
"username" : "admin"
}
}
}

我有这个代码:

class HistoryBookingViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var tableView: UITableView!

var historyBooking: [BookingHall] = []

var userHistoryRef: DatabaseReference!

override func viewDidLoad() {
super.viewDidLoad()

userHistoryRef = Database.database().reference()

loadHistoryBooking()

}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return historyBooking.count

}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: "historyCell", for: indexPath) as! HistoryBookingTableCell

// cell.studioHallName.text = historyBooking[indexPath.row].studioHallName

return cell

}

func loadHistoryBooking() {

if let userID = Auth.auth().currentUser?.uid {

userHistoryRef.child("users").child(userID).child("historyBooking").observe(.value, with: { (snapshot) in

let value = snapshot.value as! NSDictionary

print("value - \(value)")

self.tableView.reloadData()

})
}
}
}

在控制台中我可以看到所有数据,但是如何将 firebase 中的所有数据应用到我的

我的类(class)在这里:

class BookingHall {

var ref: DatabaseReference!
var studioHallName: String = ""
var studioHallAddress: String = ""
var bookingDate: String = ""
var bookingTime: String = ""
var totalSum: String = ""
var name: String = ""
var surename: String = ""
var email: String = ""
var phonenumber: String = ""

init(snapshot: DataSnapshot) {

ref = snapshot.ref

let value = snapshot.value as! NSDictionary

}
}

我知道我的loadHistoryBooking()函数中缺少代码,请告诉我要在函数中添加哪些内容> 在我的类(class)中。

最佳答案

在你的类中,你可以创建像这样的自定义 init 方法

//注意:这里缺少一些值

class Model {
let bookingDate: String
let bookingTime: String
let email: String
let name: String
let phonenumber: String
let studioHallAddress: String
let studioHallName: String
let surename: String
let totalSum: String

init(bookingDate: String, bookingTime: String, email: String, name: String, phonenumber: String, studioHallAddress: String, studioHallName: String, surename: String, totalSum: String) {
self.bookingDate = bookingDate
self.bookingTime = bookingTime
self.email = email
self.name = name
self.phonenumber = phonenumber
self.studioHallAddress = studioHallAddress
self.studioHallName = studioHallName
self.surename = surename
self.totalSum = totalSum
}
}

您可以创建具有给定详细信息的对象并将其添加到全局数组中,然后您可以显示到表格 View

关于ios - 从firebase数据库中检索数据到tableView中的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48781040/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com