简体   繁体   English

使用Swift 3从Firebase检索的数据填充TableView

[英]Populating TableView with retrieved data from Firebase with Swift 3

I've tried to retrieve data and set it into a tableview; 我试图检索数据并将其设置为表格视图; I haven't really succeeded on this. 我还没有真正做到这一点。

I want to fetch image and text into the tableView from firebase; 我想从firebase中获取图像和文本到tableView中;

import UIKit
import Firebase

class HomePageViewController: UIViewController, UITableViewDelegate {

    @IBOutlet weak var homePageTableView: UITableView!

    var ref: DatabaseReference!
    var imageFiles = [Data]()
    var price = [String]()
    var refHandle:UInt!






    override func viewDidLoad() {
        super.viewDidLoad()

        ref = Database.database().reference()



        fetchUsers()

func fetchUsers() {

        refHandle = ref.child("Users").observe(.childAdded, with: { (<#DataSnapshot#>) in
            if let retrivedData = DataSnapshot.value(String : AnyObject) {
              ??? 
            }
        })

I'm aware of this is unfinished but I have no clue of how to continue from this point. 我知道这还没有完成,但是我不知道如何从这一点继续。 How do I set it to the table??? 如何将其设置在桌子上??? and how Do I retrieve the data correctly. 以及如何正确检索数据。 Can't find any documentation about this with the newest update firebase SDK and swift 3.0 找不到具有最新更新的Firebase SDK和Swift 3.0的与此有关的任何文档

The best practice is to feed the retrieved data into model objects and supply to the table view. 最佳实践是将检索到的数据输入模型对象并提供给表视图。

This is how modal objects looks like: 这是模态对象的样子:

    class Person {
    var firstname: String
    var lastname: String

    init(firstname: String, lastname: String) {
        self.firstname = firstname
        self.lastname = lastname
    }
}

Now you have to feed the JSON data you got from firebase to this model objects. 现在,您必须将从firebase获得的JSON数据馈送到此模型对象。 Once your model object is ready, you have to load the tableview. 一旦模型对象准备就绪,就必须加载表格视图。 Tableview has data source methods. Tableview具有data source方法。 Use them to show how to populate data in tableview. 使用它们来显示如何在表视图中填充数据。

 func numberOfSections(in tableView: UITableView) -> Int {
            return 1
        }
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return modelObjectsArray.count
        }

        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell")!
            let modelObject = self.modelObjectsArray[indexPath.row] as! Person
            cell.textLabel?.text = modelObject.firstname
            return cell
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM