从Firebase读取到表视图

时间:2017-11-21 13:09:49

标签: swift firebase firebase-realtime-database

尝试显示我的"消息"在表格视图中,由于某种原因,它没有显示,尝试了所有内容,无法看到我的问题。

感谢任何帮助。

class StatusViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var dataView: UITableView!

var array = [String]()

var ref:FIRDatabaseReference?
var databaseHandle:FIRDatabaseHandle?

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view:

    // Setting firebase database reference:
    ref = FIRDatabase.database().reference()

    // Retrieve the users
    databaseHandle = ref?.child("message").observe(.childAdded, with: { (snapshot) in

        if let item = snapshot.value as? String{
            self.array.append(item)
            self.dataView.reloadData()
        }

    })

    self.dataView.delegate = self
    self.dataView.dataSource = self

}

// "$uid === auth.uid"

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = dataView.dequeueReusableCell(withIdentifier: "PostCell")! as UITableViewCell
    cell.textLabel?.text = array[indexPath.row]
    return cell

}

// Back To App View Controller:
@IBAction func BackToStatus(_ sender: Any) {
    self.dismiss(animated: true, completion: nil)
    self.performSegue(withIdentifier: "BackToStatus", sender: nil)
}

}

数据库规则

enter image description here

我试图显示的用户和消息

enter image description here

0 个答案:

没有答案