从Firestore填充UITableView

时间:2018-04-11 04:54:29

标签: swift uitableview google-cloud-firestore

这感觉非常基本,但我不能让它工作。从FIrestore读取的内容不会显示在tableview中。从我可以收集的内容中,在从FIrestore填充scheduleArray数组之前呈现tableview。如果我使scheduleArray字面值一切正常。 loadData()函数中的print语句打印出db的内容就好了。因此,与Firestore的连接正在按预期工作。

如何在呈现tableview之前获取db read和array population?

class ListScheduleViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var scheduleIDarray = [String]() // <-- If this is made an array literal, everything works
    var db: Firestore!
    override func viewDidLoad() {
        super.viewDidLoad()

        db = Firestore.firestore()
        loadData()

    }
    func loadData() {
        db.collection("schedules").getDocuments() { (querySnapshot, err) in
            if let err = err {
                print("Error getting documents: \(err)")
            } else {
                for document in querySnapshot!.documents {

                    self.scheduleIDarray.append(document.documentID)
                }
            }
            print(self.scheduleIDarray) // <-- This prints the content in db correctly
        }
    }
       @IBOutlet weak var tableView: UITableView!

    //Tableview setup
    func numberOfSections(in tableView: UITableView) -> Int {

        return 1
    }

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

                print("Tableview setup \(scheduleIDarray.count)")
        return scheduleIDarray.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "customScheduleCell", for: indexPath) as! customScheduleviewCell
        let schedule = scheduleIDarray[indexPath.row]

        cell.scheduleName.text = schedule
        print("Array is populated \(scheduleIDarray)")
        return cell
    }

}

3 个答案:

答案 0 :(得分:2)

在viewController中创建tableView的出口,如下所示: @IBOutlet weak var tableView: UITableView!并在viewDidLoad()

中写下给定的行
override func viewDidLoad() {
    super.viewDidLoad()

    tableView.dataSource = self
    tableView.delegate = self
}

成功接收数据后重新加载tableView。

答案 1 :(得分:1)

这是工作代码,它可能会帮助别人。没有检查,没有安全,只有一个非常基本但有效的代码。

class ListScheduleViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var scheduleIDarray = [String]()
    var db: Firestore!

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.dataSource = self
        tableView.delegate = self
        db = Firestore.firestore()
        loadData()

    }
    func loadData() {
        db.collection("schedules").getDocuments() { (querySnapshot, err) in
            if let err = err {
                print("Error getting documents: \(err)")
            } else {
                for document in querySnapshot!.documents {

                    self.scheduleIDarray.append(document.documentID)
                }
            }
            print(self.scheduleIDarray) 

            self.tableView.reloadData()
        }

    }
       @IBOutlet weak var tableView: UITableView!

    //Tableview setup
    func numberOfSections(in tableView: UITableView) -> Int {

        return 1
    }

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

                print("Tableview setup \(scheduleIDarray.count)")
        return scheduleIDarray.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "customScheduleCell", for: indexPath) as! customScheduleviewCell
        let schedule = scheduleIDarray[indexPath.row]

        cell.scheduleName.text = schedule
        print("Array is populated \(scheduleIDarray)")

        return cell
    }

}

答案 2 :(得分:0)

我可以看到代码中缺少2件事

1 - 您需要设置Navigation UITableView才能将数据加载到datasource

2 - 在从tableView获取所需数据后,您需要使用Firestone重新加载数据,它会调用tableView.reloadData()方法响应以填充您的tableViewDataSource

相关问题