Home>
Development environment

Xcode11.0
Swift5.1
firebase3

I have implemented a matching app function.
You are registered with Firebase.

I want to call data from Firebase and display the data one by one in the Label placed in the TableViewCell

-Call the data registered in Firebase and store it in the array
・ Store the stored data one by one in the Label in the TableViewCell and display it

Error message

You can call data from Firebase
However, it cannot be displayed in the Label inside the TableViewCell
I tried to display in TableView (without Label), but it was not displayed.

import UIKit
import Firebase
import SwiftyJSON
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    @IBOutlet weak var TableView: UITableView!
    let ref: DatabaseReference = Database.database (). reference ()
    // prepare an empty array
    var Array_Title: [String] = []
    override func viewDidLoad () {
        super.viewDidLoad ()
        ref.child ("Sample"). observe (.value, with: {(snapshot: DataSnapshot) in
            // Get Firebase data in JSON format
            let getjson = JSON (snapshot.value as? [String: AnyObject] ?? [:])
            // Do nothing if the data is 0
            if getjson.count == 0 {
                return
            }
            for (key, _) in getjson.dictionaryValue {
                // Store the called data in an array
                self.Array_Title.append ("\ (getjson [key] [" Title "]. stringValue)")
            }
// Display array on console
            print (self.Array_Title)
            self.TableView.reloadData ()
        })
    }
    // count array and return number of cells
    func tableView (_ tableView: UITableView, numberOfRowsInSection section: Int)->Int {
        return Array_Title.count
    }
    func tableView (_ tableView: UITableView, cellForRowAt indexPath: IndexPath)->UITableViewCell {
        let cell = tableView.dequeueReusableCell (withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell
        // Enter a value for the cell label.
        cell.Title.text = Array_Title [indexPath.row]
        return cell
    }
}
import UIKit
class TableViewCell: UITableViewCell {
    @IBOutlet weak var Title: UILabel!
    override func awakeFromNib () {
        super.awakeFromNib ()
        // Initialization code
    }
    override func setSelected (_ selected: Bool, animated: Bool) {
        super.setSelected (selected, animated: animated)
        // Configure the view for the selected state
    }
}
  • Answer # 1

    https://www.youtube.com/watch?v=zgVW3s9W88k

    I did it after referring to the link above!
    Thank you very much.

  • Answer # 2

    cell.ViewCellLabel.text = Array_Title [indexPath.row]

    Is this strange? ? ? Is it an error?

    If you look at Custom cell,

    @IBOutlet weak var Title: UILabel!

    so

    cell.Title.text = ...


    Isn't it necessary to write? ? ?

Related articles