简体   繁体   中英

Unable To Output Array Items Retrieved From Firebase To UITableView In Swift

If I use the static array that has the basic strings in it, I can render those out to the UITableView, BUT when I build my "jokes" array and use that for the datasource for the table cells, I just get empty list in UI. Rows output, but no data.

I'm sure it's something simple, still new to Swift (.net background).

Why are the "jokes" array items not displaying in the tableViewCell?

import UIKit
import Firebase

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {


var dict : [String: AnyObject] = [:]
var jokes = [Jokes]()

private let myArray: NSArray = ["First","Second","Third"]
private var myTableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()

    var ref = Database.database().reference()
            ref.observe(.value, with: { (snapshot) in
                for child in snapshot.children { //even though there is only 1 child
                    let snap = child as! DataSnapshot
                    //                let dict = snap.value as? NSDictionary
                    self.dict = snap.value as! [String: AnyObject]
                    for (joke, item) in self.dict ?? [:] {
                        let myJokeCollection = Jokes(fromDict: item as! [String : AnyObject]); //This has object from Dictionary retrieved from firebase
                        self.jokes.append(myJokeCollection)
                        print(myJokeCollection) //This prints collection
                        print(myJokeCollection.postUser) // this prints the "post user"

                    }


                }
            })

    //Below is intial example

    let barHeight: CGFloat = UIApplication.shared.statusBarFrame.size.height
    let displayWidth: CGFloat = self.view.frame.width
    let displayHeight: CGFloat = self.view.frame.height

    myTableView = UITableView(frame: CGRect(x: 0, y: barHeight, width: displayWidth, height: displayHeight - barHeight))
    myTableView.register(UITableViewCell.self, forCellReuseIdentifier: "JokeCell")
    myTableView.dataSource = self
    myTableView.delegate = self
    self.view.addSubview(myTableView)
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print("Num: \(indexPath.row)")
//        print("Value: \(myArray[indexPath.row])") <- THis works as it's declared at the top of class
    print("Value: \(jokes[indexPath.row])") //THis prints nothing (of course)
 }

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

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "JokeCell", for: indexPath as IndexPath)
 //If I use reference to myArray below it prints those items, but not from mine "jokes"
    cell.textLabel!.text = "\(jokes[indexPath.row])"
    return cell
}

}
class Jokes {
var postUser: String?
var punchline: String?
var rating: Int?
var setup: String?

init(fromDict dict:[String: AnyObject]) {
    self.postUser = dict["PostUser"] as? String
    self.punchline = dict["Punchline"] as? String
    self.rating = dict["Rating"] as? Int
    self.setup = dict["Setup"] as? String

}
}

All your code is correct just make sure you add tableView.reloadData()

After this line

print(myJokeCollection.postUser)
tableView.reloadData()

Why is this needed?

This is needed to reload the tableView after you have added new data, the tableView is automatically loaded when the view launches but when you add new data you must call this to tell the tableView, "Oh, I've got new data! Time to reload!"

Though @OkiRules answer is correct i would like to correct one thing and that is do not reload data in loop it just increases processing time and keeps UI more busy. Reload data out of loop, Do it like below.

for (joke, item) in self.dict ?? [:] {
                        let myJokeCollection = Jokes(fromDict: item as! [String : AnyObject]); //This has object from Dictionary retrieved from firebase
                        self.jokes.append(myJokeCollection)
                        print(myJokeCollection) //This prints collection
                        print(myJokeCollection.postUser) // this prints the "post user"

                    }
    tableView.reloadData()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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