首页 > 解决方案 > 无法将数据从 Firestore 加载到 uitableview

问题描述

我能够查询数据并将其与我的模型相匹配,但无法在我的表格视图中显示它。除了情节提要外,我还有 3 个正在使用的文件。

这是主视图控制器:

class MealplanViewController: UIViewController {

var db: Firestore!
var mealplanArray = [Mealplan]()

@IBOutlet weak var mealplanTableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()

    mealplanTableView?.dataSource = self
    mealplanTableView?.delegate = self

    db = Firestore.firestore()
    loadData()
    // Do any additional setup after loading the view.
}

func loadData() {
    userEmail = getUserEmail()
    db.collection("Meal_Plans").getDocuments() {querySnapshot , error in
        if let error = error {
            print("\(error.localizedDescription)")
        } else {
            self.mealplanArray = querySnapshot!.documents.compactMap({Mealplan(dictionary: $0.data())})
            print(self.mealplanArray)
            DispatchQueue.main.async {
                self.mealplanTableView?.reloadData()
            }
        }
    }
}

func getUserEmail() -> String {
    let user = Auth.auth().currentUser
    if let user = user {
        return user.email!
    } else {
        return "error"
    }
}
}

// MARK: - Table view delegate

extension MealplanViewController: UITableViewDataSource, UITableViewDelegate {


func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return mealplanArray.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    //let cell = tableView.dequeueReusableCell(withIdentifier: "MealplanTableViewCell", for: indexPath)
    let mealplanRow = mealplanArray[indexPath.row]
    let cell = tableView.dequeueReusableCell(withIdentifier: "MealplanTableViewCell") as! MealplanTableViewCell

    cell.setMealplan(mealplan: mealplanRow)
    return cell
}
}

这是我显示查询值之一的单元格:

class MealplanTableViewCell: UITableViewCell {

@IBOutlet weak var mealplanNameLabel: UILabel!


func setMealplan(mealplan: Mealplan) {
    // Link the elements with the data in here
    mealplanNameLabel.text = mealplan.mpName
    print(mealplan.mpName)
}

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
}

}

最后,这是数据模型:

import Foundation
import Firebase

protocol MealplanSerializable {
    init?(dictionary:[String:Any])
}

struct Mealplan {

    var mealplanId:String

    var mpName:String

    ]
}
}

extension Mealplan : MealplanSerializable {
    init?(dictionary: [String : Any]) {
        guard let 
        let mealplanId = dictionary["mealplanId"] as? String,

        let mpName = dictionary["mpName"] as? String,


    else { return nil }

    self.init(mealplanId: mealplanId, mpName: mpName)
}
}

我得到的只是一个没有数据的空表视图。

标签: swiftgoogle-cloud-firestore

解决方案


推荐阅读