我正在尝试从我的数据库中获取用户的数据。我需要在当前授权用户的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 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()
函数中缺少代码,请告诉我在函数和我的<中添加的内容强>类
答案 0 :(得分:0)
在您的课程中,您可以像这样创建自定义初始化方法
//注意:这里缺少一些值
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
}
}
您可以使用给定的详细信息创建对象并将其添加到全局数组中,然后可以显示到tableview