Firebase retrieve multiple data using UITableViewCell (Swift 3)

Viewed 569

I'm in trouble with retrieving data from Firebase. My JSON looks like this:

-user
  -contacts
     -autoID Value1
        -contactName : A
     -autoID Value2
        -contactName : B
     -autoID Value3
        -contactName : C
(more contactName is coming, in same structure.)

But I have no idea how to get ALL contactName's Data(A,B,C....) then print in UITableView in Swift3.0

I want to make a result like this :

@In the TableView@

A

B

C

...

But My result is different, Maybe some problem in TableViewCell.swift... but I'm not sure

ViewController.swift :

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return 1
}

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

    let cell : MoneyTableViewCell = tableView.dequeueReusableCell(withIdentifier: "MoneyTableViewCell", for: indexPath) as! MoneyTableViewCell

    cell.eachCell()
    cell.layoutIfNeeded()

    return cell
}

MoneyTableViewCell.swift

func eachCell() {

    dbRef = Database.database().reference()

    self.dbRef.child("user/contacts").observe(.value, with: {(snapshot) in

        if let result = snapshot.children.allObjects as? [DataSnapshot] {

            for child in result {

                let receiverNameDB = child.childSnapshot(forPath: "contactName").value! as! String
                self.nameArray.append(receiverNameDB)

                self.nameLabel.text = receiverNameDB
                print(self.nameArray)

                continue

            }
        }
    })
}

Can Anybody help me please ;)

Update - I solved this problem! Here is my edited code... Thanks to @Abdul91

The problem was.. I had to get the data from DB first, then put them in the array. After that get each data for tableView.

My edited code will tell you more.

ViewController.swift:

var nameArray:[String] = []

override func viewDidLoad() {
    super.viewDidLoad()

    getNameDB{ value in
        self.nameArray = value
        self.tableView.reloadData()
    } 


    tableView.register(UINib(nibName: "MoneyTableViewCell", bundle: nil), forCellReuseIdentifier: "MoneyTableViewCell")
}

//completion is for getting DB value outside of block.. 
func getNameDB(completion: @escaping (_ value: [String]) -> Void) {

    self.dbRef.child("user/contacts").observe(.value, with: {(snapshot) in

        if let result = snapshot.children.allObjects as? [DataSnapshot] {

            for child in result {

                let receiverNameDB = child.childSnapshot(forPath: "contactName").value! as! String
                self.nameArray.append(receiverNameDB)
            }
            completion(self.nameArray)
        }
    })
}



func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return self.nameArray.count
}

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

    let cell : MoneyTableViewCell = tableView.dequeueReusableCell(withIdentifier: "MoneyTableViewCell", for: indexPath) as! MoneyTableViewCell

    cell.nameLabel.text = nameArray[(indexPath as NSIndexPath).row]
    cell.layoutIfNeeded()

    return cell
}
0 Answers
Related