我正在尝试使用Firebase Realtime数据库中的数据填充表格视图 [英] I'm trying to populate my tableview with data from the Firebase Realtime database

查看:64
本文介绍了我正在尝试使用Firebase Realtime数据库中的数据填充表格视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以将当​​前的tableview数据加载到数据库中,然后将新数据打印到控制台上,但是无法将新数据重新返回到tableview中,因为我知道它应该很简单,所以我不遗余力!

I can load my current tableview data onto the database and then print out the new data onto my console but can't get the new data back into the tableview and I'm tearing my hair out because I know it should be simple!

我已经尝试了各种各样的方法,但是我只是想不出我要去哪里.

I've tried all sorts of things but I just can't figure out where I'm going wrong.

//Saves to database without any problems
//Class
var ref: DatabaseReference!
//ViewDidLoad
ref = Database.database().reference()

func save()
{
let ref = Database.database().reference(withPath: "Admin")

let adding = ref.child(me)

let addData: [String: [String]] = ["addJokes": data]

adding.setValue(addData)
{
    (error:Error?, ref:DatabaseReference) in
    if let error = error
    {
        print("Data could not be saved: \(error).")
    }
    else
    {
        print("Data saved successfully!")
    }
}
}

可以将数据库数据打印到我的控制台,但无法将其放入我的表视图中

Can print out the database data to my console but can't get it into my tableview

let ref = Database.database().reference(withPath: "Admin")

    ref.observe(.value, with:
        {
            (snapshot) in

            let new = snapshot.value as? String

            print(snapshot.value as Any)

            if let newData = new
            {
                self.data.append(newData)
                self.mainTable.reloadData()
            }
    })

更新

TableView详细信息-

Update

TableView details-

TableView类扩展

TableView Class Ext

extension TableView: UITableViewDataSource, UITableViewDelegate

{

    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        if isSearching {
             return filteredArray.count
        }
        else
        {
            return data.count
        }
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        var array: String?
        if isSearching
        {
            array = filteredArray[indexPath.row]
        }
        else
        {
            array = data[indexPath.row]
        }
        let cell = mainTable.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as UITableViewCell
      cell.textLabel?.text = array
        return cell
    }

TableView类-

TableView Class-

class TableView: UIViewController

{

    let cellId = "cellId"

    var filteredArray = [String]()

    var ref: DatabaseReference!

    var data = [

        """
       multiple line 
       data array
        """
       ]

    lazy var mainTable: UITableView =
        {
            let table = UITableView()
            table.translatesAutoresizingMaskIntoConstraints = false
            table.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
            return table
    }()
        override func viewDidLoad() {
        super.viewDidLoad()
        mainTable.delegate = self
        mainTable.dataSource = self
}

控制台将我想要的内容完全打印回到表视图中.将打印功能转换为结果通常是容易的部分.

Console prints exactly what I want back into my tableview. Turning print function into results is usually the easy part.

推荐答案

问题出在let new = snapshot.value as? String.在这里,new为null,因此if let newData = new始终为false,并且如果不执行块.首先,检查snapshot.value的数据类型和值,然后相应地使用它.

The problem lies in let new = snapshot.value as? String. Here, new is null thus if let newData = new is always false and if block won't be executed. First, check snapshot.value's data type and value then use it accordingly.

这篇关于我正在尝试使用Firebase Realtime数据库中的数据填充表格视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆