从 Firebase 实时数据库获取数据到 Tableview 单元格

问题描述

我正在从 firebase 实时数据库中检索数据并使用它来设置表格视图单元格中的标签文本。现在,每当我在模拟器和/或在我的 iPhone 上打开 tableview 时,它都会出现空白。我已经确保可重用标识符是正确的,TableViewController 的名称在 swift 文件和故事板等中匹配。我已经运行了一些测试试图查看出了什么问题,我认为代码只是没有阅读数据库中的数据,即使我知道它在那里。这是 TableViewController 的当前代码


import UIKit
import FirebaseDatabase
import FirebaseAuth

class RemoveSpots: UITableViewController {
    
    var spotRef:DatabaseReference?

    @IBOutlet weak var spotView: UITableView!
    
    var postData = [SpotModel]()
    
    override func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell",for: indexPath) as! SpotCell
        
        let spot: SpotModel
        spot = postData[indexPath.row]
        cell.TypeLabel.text = spot.type
        cell.PriceLabel.text = spot.price
        cell.AvailLabel.text = spot.avail
        cell.ContactLabel.text = spot.contact
        
        return cell
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        spotView.delegate = self
        spotView.dataSource = self
        let userID = Auth.auth().currentUser?.uid
        spotRef = Database.database().reference().child("\(userID)")
        fillTable()
        spotView.reloadData()
    }
    
    func fillTable() {
        spotRef?.observe(.childAdded,with: { (snapshot) in
        
                for spots in snapshot.children.allObjects as![DataSnapshot] {
                    let spotObject = spots.value as? [String: AnyObject]
                    let spottype = spotObject?["Type"]
                    let spotPrice = spotObject?["Price"]
                    let spotAvail = spotObject?["Availability"]
                    let spotContact = spotObject?["Contact"]
                    let cell = SpotModel(type: spottype as! String,price: spotPrice as! String,avail: spotAvail as! String,contact: spotContact as! String)
                    self.postData.append(cell)
                }
                self.spotView.reloadData()
        })
    }
}

这是自定义单元格的代码



import UIKit

class SpotCell: UITableViewCell {

    
    @IBOutlet weak var ContactLabel: UILabel!
    @IBOutlet weak var AvailLabel: UILabel!
    @IBOutlet weak var PriceLabel: UILabel!
    @IBOutlet weak var TypeLabel: UILabel!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool,animated: Bool) {
        super.setSelected(selected,animated: animated)

        // Configure the view for the selected state
    }

}

连同第二个 swift 文件


class SpotModel {
    var type: String?
    var price: String?
    var avail: String?
    var contact: String?
    
    init(type: String?,price: String?,avail: String?,contact: String?) {
        self.type = type
        self.price = price
        self.avail = avail
        self.contact = contact
    }
}

我不知道这里出了什么问题,已经解决了四分之一天,认为解决方案一定很简单,并且会以永恒的感激之情回报任何帮助。 Here is an example of the database layout. This is one User ID,with a few entries (titles with random numbers) within it. I am only trying to access the children of one user ID at a time (that of the current user),as you can see by the code

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)