FireBase返回的字符串为nil

时间:2017-12-22 02:57:18

标签: ios swift uitableview firebase swift3

Man,UITableView&只是不想和我合作。我一直在输出字符串到Firebase存储,我看到存储的值。我的问题是我无法将任何字符串输出到UITableView中。我只看到一张空白的桌子视图,当我去看看thoughtObjects.thoughts输出的内容时,它只是说" nil。"如果有人能帮助弄清楚哪些是错的,那将是值得赞赏的。非常感谢StackOverflow。

import UIKit
import Firebase
import FirebaseDatabase

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var inputThoughtTextField: UITextField!
@IBOutlet weak var successfulUploadLbl: UILabel!

@IBOutlet weak var tableView: UITableView!

var refThoughts: DatabaseReference!
var thoughtList = [ThoughtModel]()

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


public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
    //creating a cell using the custom class
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! VCTableViewCell

    //the artist object
    let thoughtObjects: ThoughtModel

    //getting the artist of selected position
    thoughtObjects = thoughtList[indexPath.row]

    //adding values to labels
    cell.wordCloud.text = thoughtObjects.thoughts
    //print(cell.wordCloud.text)
    //returning cell
    return cell
}

override func viewDidLoad() {
    super.viewDidLoad()

    refThoughts = Database.database().reference().child("thoughts");

    refThoughts.observe(DataEventType.value, with: { (snapshot) in

        //if the reference have some values
        if snapshot.childrenCount > 0 {

            //clearing the list
            self.thoughtList.removeAll()

            //iterating through all the values
            for thoughts in snapshot.children.allObjects as! [DataSnapshot] {
                //getting values
                let thoughtsObject = thoughts.value as? [String: AnyObject]
                let thoughtText  = thoughtsObject?["thoughts"]
                let thoughtId  = thoughtsObject?["id"]

                //creating artist object with model and fetched values
                let thoughtCreation = ThoughtModel(id: thoughtId as! String?, thoughts: thoughtText as! String?)

                //appending it to list
                self.thoughtList.append(thoughtCreation)
            }

            //reloading the tableview
            self.tableView.reloadData()
        }
    })
}

@IBAction func buttonAddThought(_ sender: UIButton){
    addThought()
}

func addThought(){
    let key = refThoughts.childByAutoId().key

    let thought = ["id": key,
                   "Thoughts": inputThoughtTextField.text! as String
                    ]
    refThoughts.child(key).setValue(thought)
    successfulUploadLbl.text = "Thought Uploaded"
}

}

0 个答案:

没有答案